コード例 #1
0
        // GET: Admin/Products/Edit/5
        public async Task <IActionResult> Edit(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var product = await _pRepository.GetAll()
                          .Include(e => e.Category)
                          .SingleOrDefaultAsync(m => m.Id == id);

            if (product == null)
            {
                return(NotFound());
            }
            var viewModel = new ProductsEditVm
            {
                Id                  = product.Id,
                Name                = product.Name,
                Description         = product.Description,
                PhotoUrl            = product.PhotoUrl,
                Price               = product.Price,
                SortNumber          = product.SortNumber,
                CategoryId          = product.Category.Id,
                AvailableCategories = _cRepository.GetAll().OrderBy(e => e.Name)
            };

            return(View(viewModel));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(long id, ProductsEditVm editVm)
        {
            if (id != editVm.Id)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                try
                {
                    Category category = await _cRepository.GetByIdAsync(editVm.CategoryId.Value);

                    if (category != null)
                    {
                        Product updatedProduct = await _pRepository.GetByIdAsync(editVm.Id);

                        updatedProduct.Id          = editVm.Id;
                        updatedProduct.Name        = editVm.Name;
                        updatedProduct.Description = editVm.Description;
                        updatedProduct.Price       = editVm.Price;
                        updatedProduct.SortNumber  = editVm.SortNumber;
                        updatedProduct.Category    = category;

                        if (editVm.UploadedImage != null)
                        {
                            DeleteProductImage(updatedProduct);
                            updatedProduct.PhotoUrl = await SaveProductImage(editVm.UploadedImage);
                        }

                        TempData[Constants.SuccessMessage] = $"Product \"{updatedProduct.Name}\" has been updated";
                        await _pRepository.UpdateAsync(updatedProduct);
                    }
                    else
                    {
                        ModelState.AddModelError(nameof(editVm.CategoryId), "This category doesn't exist");
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(editVm.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            editVm.AvailableCategories = _cRepository.GetAll().OrderBy(e => e.Name);
            return(View(editVm));
        }