Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async Task<IActionResult> Edit(int id)
- {
- var product = await this.productService.GetProductAsync(id);
- if (product == null) return NotFound();
- var model = mapper.MapFrom(product);
- model = await EditDropdown(model);
- return View(model);
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<IActionResult> Edit(int id, ProductViewModel product)
- {
- if (!this.ModelState.IsValid)
- {
- product = await EditDropdown(product);
- return View(product);
- }
- try
- {
- if (product.ProductImage != null)
- {
- if (!IsValidImage(product.ProductImage))
- {
- StatusMessage = "Error: Please provide a .jpg or .png file smaller than 2MB";
- return View(product);
- }
- product.ImageName = await productService.UploadProductImageAsync(
- GetUploadsRoot(),
- product.ProductImage.FileName,
- product.ProductImage);
- }
- if (id != product.ProductId) return NotFound();
- var newProduct = product.ToEntity();
- await productService.UpdateProductAsync(newProduct);
- StatusMessage = $"Successfully modified \"{product.ProductName}\"";
- return RedirectToAction(nameof(Index));
- }
- catch (DbUpdateConcurrencyException)
- {
- if (await productService.ExistsInDBAsync(product.ProductName) == false)
- return NotFound();
- throw;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement