public void EditProduct(EditProductBm bm, int id) { Product model = this.Context.Products.Find(id); model.Category = this.Context.Categories.FirstOrDefault(c => c.Title == bm.Category); model.Description = bm.Description; model.ImageUrl = bm.ImageUrl; model.Model = bm.Model; model.Price = bm.Price; model.Quantity = bm.Quantity; model.Title = bm.Title; this.Context.SaveChanges(); }
public ActionResult Edit(EditProductBm bm, int id) { if (!this.ModelState.IsValid) { return(this.View(bm)); } try { this.service.EditProduct(bm, id); return(RedirectToAction("All")); } catch (DbEntityValidationException ex) { var error = ex.EntityValidationErrors.First().ValidationErrors.First(); this.ModelState.AddModelError(error.PropertyName, error.ErrorMessage); IEnumerable <AllCategoriesVm> categories = this.service.GetAllCategoriesTitles(); EditProductVm vm = this.service.GetProductById(id); vm.Categories = categories; return(this.View(vm)); } }