public ActionResult Edit(int id, SubCatUpdate model) { if (!ModelState.IsValid) { ViewBag.CategoryId = BuildCategoryDropdown(model.CategoryId); return(View(model)); } if (model.SubCatId != id) { ModelState.AddModelError("", "Id Mismatch"); ViewBag.CategoryId = BuildCategoryDropdown(model.CategoryId); return(View(model)); } var service = CreateSubCatService(); if (service.UpdateSubCat(model)) { TempData["SaveResult"] = $"'{model.SubCatName}' was updated."; return(RedirectToAction("Index")); } ModelState.AddModelError("", $"'{model.SubCatName}' could not be updated."); ViewBag.CategoryId = BuildCategoryDropdown(model.CategoryId); return(View(model)); }
// GET: SubCategory/Update public ActionResult Edit(int id) { var service = CreateSubCatService(); var detail = service.GetSubCatById(id); var model = new SubCatUpdate { SubCatId = detail.SubCatId, CategoryId = detail.CategoryId, SubCatName = detail.SubCatName, SubCatMaxAllowed = detail.SubCatMaxAllowed }; ViewBag.CategoryId = BuildCategoryDropdown(model.CategoryId); return(View(model)); }
public bool UpdateSubCat(SubCatUpdate model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .SubCategories .Single(e => e.SubCatId == model.SubCatId); entity.CategoryId = model.CategoryId; entity.SubCatName = model.SubCatName; entity.SubCatMaxAllowed = model.SubCatMaxAllowed; bool success = true; try { ctx.SaveChanges(); } catch { success = false; } return(success); } }