public ActionResult AddCategory(CategoryModel category) { var Pcategory = new Product_Category { Type = category.Main_Category.Type, Description = category.Main_Category.Description }; db.Product_Category.Add(Pcategory); db.SaveChanges(); int id = Pcategory.ID; try { for (int i = 0; i < category.New_Sub_Category.Count(); i++) { var NewSubCategory = new Sub_Product_Category { PC_ID = id, Type = category.New_Sub_Category[i].Type, Description = category.New_Sub_Category[i].Description }; db.Sub_Product_Category.Add(NewSubCategory); db.SaveChanges(); } }catch (Exception ex) { } return(RedirectToAction("MaintainCategory")); }
public ActionResult EditCategory(CategoryModel category) { var mainCategory = db.Product_Category.Find(category.Main_Category.ID); mainCategory.Type = category.Main_Category.Type; mainCategory.Description = category.Main_Category.Description; db.SaveChanges(); try { try { for (int i = 0; i < category.Sub_Category.Count(); i++) { if (category.Sub_Category[i].Deleted == true) { var subCategory = db.Sub_Product_Category.Find(category.Sub_Category[i].ID); db.Sub_Product_Category.Remove(subCategory); db.SaveChanges(); } else { var subCategory = db.Sub_Product_Category.Find(category.Sub_Category[i].ID); subCategory.Type = category.Sub_Category[i].Type; subCategory.Description = category.Sub_Category[i].Description; db.SaveChanges(); } } }catch (Exception ex) { } for (int i = 0; i < category.New_Sub_Category.Count(); i++) { var NewSubCategory = new Sub_Product_Category { PC_ID = category.Main_Category.ID, Type = category.New_Sub_Category[i].Type, Description = category.New_Sub_Category[i].Description }; db.Sub_Product_Category.Add(NewSubCategory); db.SaveChanges(); } }catch (Exception ex) { } return(RedirectToAction("MaintainCategory")); }