public ActionResult Edit(int?Id) { ViewBag.colors = db.Colors.ToList(); if (Id == null) { return(HttpNotFound()); } var prod = db.ProductColorSize.Find(Id); var product = db.Products.Where(p => p.Id == prod.ProductId).First(); if (prod == null) { return(HttpNotFound()); } int sCatId = product.SectionCategoryId; int section = db.SectionCategories.Where(p => p.Id == sCatId).First().SectionId; int category = db.SectionCategories.Where(p => p.Id == sCatId).First().CategoryId; List <Size> sizes = new List <Size>(); var sizeDb = db.CategorySizes.Where(s => s.SectionId == section && s.CategoryId == category).Select(m => new { Id = m.Size.Id, Name = m.Size.Name }).ToList(); foreach (var item in sizeDb) { sizes.Add(new Size { Id = item.Id, Name = item.Name }); } ProductCS_VM vm = new ProductCS_VM { Sizes = sizes, ProductSize = prod }; ViewBag.SizeId = new SelectList(sizes, "Id", "Name", prod.SizeId); ViewBag.ColorId = new SelectList(ViewBag.colors, "Id", "Name", prod.ColorId); return(View(vm)); }
public async Task <ActionResult> Edit(ProductCS_VM vM, ProductColorSize productColorSize) { productColorSize.ProductId = vM.ProductSize.ProductId; productColorSize.Amount = vM.ProductSize.Amount; ViewBag.colors = db.Colors.ToList(); if (productColorSize.Id == 0) { ModelState.AddModelError("allError", "Product Id is not found"); return(View(vM)); } var productDb = db.ProductColorSize.Find(productColorSize.Id); if (productDb == null) { ModelState.AddModelError("allError", "Product is not found"); return(View(vM)); } productColorSize.ProductId = productDb.ProductId; if (productColorSize.SizeId == 0 || productColorSize.ColorId == 0) { ModelState.AddModelError("allError", "Size id or color id is not found"); return(View(vM)); } productDb.ProductId = productColorSize.ProductId; productDb.SizeId = productColorSize.SizeId; productDb.ColorId = productColorSize.ColorId; productDb.Amount = productColorSize.Amount; await db.SaveChangesAsync(); return(RedirectToAction("Index")); }