Esempio n. 1
0
        public IActionResult Edit(int id)//Id Az asp-route-id miad inja
        {
            CellphoneAddEdit model = new CellphoneAddEdit();

            var category   = (from c in _context.categories select c).Where(c => c.Name.Contains("موبایل")).SingleOrDefault();
            int categoryId = category.CategoryId;

            //Dropdown List - Products
            model.Products = _context.products.Where(p => p.CategoryId == categoryId).Select(c => new SelectListItem
            {
                Text  = c.ProductName,
                Value = c.ProductId.ToString()
            }).ToList();

            if (id != 0)
            {
                //Dependency Injection
                using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>())
                {
                    Cellphone cellphone = _context.cellphones.Where(p => p.ProductId == id).SingleOrDefault();
                    if (cellphone != null)
                    {
                        model.ProductId              = cellphone.ProductId;
                        model.CellphoneId            = cellphone.CellphoneId;
                        model.CameraHas              = cellphone.CameraHas;
                        model.CameraRecording        = cellphone.CameraRecording;
                        model.CameraResolution       = cellphone.CameraResolution;
                        model.ConnectionNetworks     = cellphone.ConnectionNetworks;
                        model.ConnectionTechnologies = cellphone.ConnectionTechnologies;
                        model.CpuChipset             = cellphone.CpuChipset;
                        model.CpuCore         = cellphone.CpuCore;
                        model.CpuFrequency    = cellphone.CpuFrequency;
                        model.GPU             = cellphone.GPU;
                        model.SimcardCount    = cellphone.SimcardCount;
                        model.CpuType         = cellphone.CpuType;
                        model.ScreenProtector = cellphone.ScreenProtector;
                        model.SimcardDesc     = cellphone.SimcardDesc;
                        model.StorageSupport  = cellphone.StorageSupport;
                        model.Description     = cellphone.Description;
                        model.OS               = cellphone.OS;
                        model.OSVersion        = cellphone.OSVersion;
                        model.RAM              = cellphone.RAM;
                        model.ScreenSize       = cellphone.ScreenSize;
                        model.ScreenTechnology = cellphone.ScreenTechnology;
                        model.ScreenType       = cellphone.ScreenType;
                        model.Size             = cellphone.Size;
                        model.Storage          = cellphone.Storage;
                        model.StorageType      = cellphone.StorageType;
                        model.Weight           = cellphone.Weight;
                    }
                }
            }
            return(View("Add", model));
        }
Esempio n. 2
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Add(int CellphoneId, CellphoneAddEdit model, IEnumerable <IFormFile> files)
        {
            if (ModelState.IsValid)
            {
                /***********************/
                if (CellphoneId == 0)
                {
                    //Insert
                    using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>())
                    {
                        Cellphone cellphoneModel = AutoMapper.Mapper.Map <CellphoneAddEdit, Cellphone>(model);
                        db.cellphones.Add(cellphoneModel);
                        db.SaveChanges();
                    }
                    return(Json(new { status = "success", message = "جزییات لپ‌تاپ با موفقیت ثبت شد" }));
                }
                else
                {
                    //Update
                    using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>())
                    {
                        Cellphone cellphoneModel = AutoMapper.Mapper.Map <CellphoneAddEdit, Cellphone>(model);
                        db.cellphones.Update(cellphoneModel);
                        db.SaveChanges();
                    }
                    return(Json(new { status = "success", message = "جزییات لپ‌تاپ  با موفقیت ویراش شد" }));
                }
            }

            var category   = (from c in _context.categories select c).Where(c => c.Name.Contains("موبایل")).SingleOrDefault();
            int categoryId = category.CategoryId;

            //Dropdown List - Products
            model.Products = _context.products.Where(p => p.CategoryId == categoryId).Select(c => new SelectListItem
            {
                Text  = c.ProductName,
                Value = c.ProductId.ToString()
            }).ToList();

            //Display Validation with Jquery Ajax
            var list = new List <string>();

            foreach (var validation in ViewData.ModelState.Values)
            {
                list.AddRange(validation.Errors.Select(error => error.ErrorMessage));
            }

            return(Json(new { status = "error", error = list }));
        }
Esempio n. 3
0
        public IActionResult Add()
        {
            CellphoneAddEdit model = new CellphoneAddEdit();
            var category           = (from c in _context.categories select c).Where(c => c.Name.Contains("موبایل")).SingleOrDefault();
            int categoryId         = category.CategoryId;

            //Dropdown List - Products
            model.Products = _context.products.Where(p => p.CategoryId == categoryId).Select(c => new SelectListItem
            {
                Text  = c.ProductName,
                Value = c.ProductId.ToString()
            }).ToList();

            return(View("Add", model));
        }