public async Task <IActionResult> Edit(ProductFormEditModel productForm) { // no validation for now if (ModelState.IsValid) { Product product = _productRepository.GetProductById(productForm.ProductId); if (product != null) { foreach (var image in productForm.ImagesFiles) { // check image size is not greater than 8mb otherwise skip if (image.Length / 1024 / 1024 <= 8) { // get random filename and combine with the extension file camewith var uniqueFileName = Path.GetRandomFileName() + Path.GetExtension(image.FileName); var uploadPath = Path.Combine(_hostingEnvironment.WebRootPath, "ProductImages"); var filePath = Path.Combine(uploadPath, uniqueFileName); var imgUrl = await _blobService.UploadFileBlobAsync(image, uniqueFileName); // add all the iamge url to the product object productForm.Images.Add(new ProductImage { Image = imgUrl }); } } _productRepository.EditProduct(product, productForm); /// change this below after test return(Json(new { success = "true" })); } else { return(NotFound()); } } var errorList = ModelState.ToDictionary( kvp => kvp.Key, kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray() ); return(Json(new { Errors = errorList, success = "false" })); }
public IActionResult Edit(int id) { Product product = _productRepository.GetProductById(id); var currentUserId = User.FindFirstValue(ClaimTypes.NameIdentifier); // check the product being edited is by the correct owner if (product.OwnerId.Equals(currentUserId)) { if (product != null) { ProductFormEditModel productEditForm = new ProductFormEditModel { ProductId = product.ProductId, Name = product.Name, Price = product.Price, Description = product.Description, CategoryId = product.CategoryId }; foreach (var img in product.Images) { productEditForm.ImagesUrl.Add(img.Image); } foreach (var category in _categoryRepository.AllCategory) { productEditForm.Categories.Add( new SelectListItem { Value = category.CategoryId.ToString(), Text = category.Name });; } return(View(productEditForm)); } else { return(NotFound()); } } return(NotFound()); }