Advertisement
Guest User

Untitled

a guest
May 26th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. public async Task<IActionResult> Edit(int id)
  2.         {
  3.             var product = await this.productService.GetProductAsync(id);
  4.             if (product == null) return NotFound();
  5.  
  6.             var model = mapper.MapFrom(product);
  7.             model = await EditDropdown(model);
  8.  
  9.             return View(model);
  10.         }
  11.  
  12.         [HttpPost]
  13.         [ValidateAntiForgeryToken]
  14.         public async Task<IActionResult> Edit(int id, ProductViewModel product)
  15.         {
  16.             if (!this.ModelState.IsValid)
  17.             {
  18.                 product = await EditDropdown(product);
  19.                 return View(product);
  20.             }
  21.  
  22.             try
  23.             {
  24.                 if (product.ProductImage != null)
  25.                     {
  26.                         if (!IsValidImage(product.ProductImage))
  27.                         {
  28.                             StatusMessage = "Error: Please provide a .jpg or .png file smaller than 2MB";
  29.                             return View(product);
  30.                         }
  31.  
  32.                         product.ImageName = await productService.UploadProductImageAsync(
  33.                         GetUploadsRoot(),
  34.                         product.ProductImage.FileName,
  35.                         product.ProductImage);
  36.                     }
  37.  
  38.                     if (id != product.ProductId) return NotFound();
  39.  
  40.                     var newProduct = product.ToEntity();
  41.                     await productService.UpdateProductAsync(newProduct);
  42.  
  43.                     StatusMessage = $"Successfully modified \"{product.ProductName}\"";
  44.                     return RedirectToAction(nameof(Index));
  45.             }
  46.             catch (DbUpdateConcurrencyException)
  47.             {
  48.                 if (await productService.ExistsInDBAsync(product.ProductName) == false)
  49.                     return NotFound();
  50.                 throw;
  51.             }
  52.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement