public ActionResult Create(SubCatCreate model) { if (!ModelState.IsValid) { ViewBag.CategoryId = BuildCategoryDropdown(model.CategoryId); return(View(model)); } var service = CreateSubCatService(); if (service.CreateSubCat(model)) { TempData["SaveResult"] = $"'{model.SubCatName}' was created"; return(RedirectToAction("Index")); } ; ModelState.AddModelError("", $"'{model.SubCatName}' could not be created."); ViewBag.CategoryId = BuildCategoryDropdown(model.CategoryId); return(View(model)); }
public bool CreateSubCat(SubCatCreate model) { var entity = new SubCategory() { CategoryId = model.CategoryId, SubCatName = model.SubCatName, SubCatMaxAllowed = model.SubCatMaxAllowed, CreateBy = _userId, CreateAt = DateTimeOffset.Now }; using (var ctx = new ApplicationDbContext()) { ctx.SubCategories.Add(entity); bool success = true; try { ctx.SaveChanges(); } catch { success = false; } return(success); } }