// GET: Products/Edit/5 public ActionResult Edit(Guid?productId) { if (productId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Product product = _productService.GetProductById(productId); if (product == null) { return(HttpNotFound()); } ProductToEditViewModel productToEdit = new ProductToEditViewModel() { ProductId = product.ProductId, Name = product.Name, Price = product.Price, ItemsAvailable = product.ItemsAvailable, CategoryName = product.Subcategory.Category.Name, SubcategoryName = product.Subcategory.Name }; //ViewBag.SubcategoryId = new SelectList(db.Subcategories, "SubcategoryId", "Name", product.SubcategoryId); return(View(productToEdit)); }
public void Edit(ProductToEditViewModel productToEdit) { Product product = _dbContext.Products.FirstOrDefault(c => c.ProductId == productToEdit.ProductId); product.Name = productToEdit.Name; product.Price = productToEdit.Price; product.ItemsAvailable = productToEdit.ItemsAvailable; _dbContext.Entry(product).State = EntityState.Modified; _dbContext.SaveChanges(); }
public ActionResult Edit([Bind(Include = "ProductId,Name,Price,ItemsAvailable")] ProductToEditViewModel product) { if (ModelState.IsValid) { _productService.Edit(product); return(RedirectToAction("Index")); } //ViewBag.SubcategoryId = new SelectList(db.Subcategories, "SubcategoryId", "Name", product.SubcategoryId); return(View(product)); }
public async Task <IActionResult> Update([FromBody] ProductToEditViewModel model) { if (ModelState.IsValid) { try { //Retrive the object from database var entity = _context.Products.FirstOrDefault(item => item.ProductId == Convert.ToInt32(model.ProductId)); // Validate entity is not null if (entity != null) { // Make changes on entity entity.EditDate = DateTime.Now; entity.Image = model.Image; entity.Name = model.Name; entity.ProductId = model.ProductId; entity.Sku = model.Sku; entity.Sorting = model.Sorting; // Update entity in DbSet _context.Products.Update(entity); // Save changes in database await _context.SaveChangesAsync(); return(Ok(model)); } else { return(NotFound()); } } catch (Exception ex) { if (ex.GetType().FullName == "Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException") { return(NotFound()); } return(BadRequest()); } } return(BadRequest()); }