public ActionResult CreateNewCategory(CategoryModel categoryModel) { var createCategoryContext = new YNTCTermContext(); int? termId = (int?)TempData["termId"]; var currentTerm = createCategoryContext.Term.Find(termId); currentTerm.Categories.Add(categoryModel); if (ModelState.IsValid) { createCategoryContext.Entry(categoryModel).State = EntityState.Added; createCategoryContext.SaveChanges(); return RedirectToAction("MonthOverview", currentTerm); } return View("CreateNewCategoryView", categoryModel); }
private TermModel GetNewTerm() { TermModel term = new TermModel(); using (var db = new YNTCTermContext()) { TermModel latestTerm = db.Term.OrderByDescending(e => e.Id).FirstOrDefault(); term.PrevId = latestTerm.Id; //term.NextId = term.Id; db.SaveChanges(); term.StartDate = latestTerm.StartDate.AddMonths(1); term.ProjectedGoal = latestTerm.ProjectedGoal; term.Categories = new List<CategoryModel>(); foreach(CategoryModel catm in latestTerm.Categories)//perform deep copy { CategoryModel new_catm = new CategoryModel { NameOfCategory = catm.NameOfCategory, Customers = new List<CustomerModel>() }; foreach(CustomerModel custm in catm.Customers) { CustomerModel new_custm = new CustomerModel { NameOfCompany = custm.NameOfCompany, BudgetActualCustomer = new BudgetActualModel { Budget = 0, Actual = 0, Difference = 0 } }; new_catm.Customers.Add(new_custm); } term.Categories.Add(new_catm); } } return term; }