public ActionResult Create(BudgetCategoryCreate model) { var ctx = new ApplicationDbContext(); var budget = ctx.Budgets.Find(model.BudgetId); if (budget == null) { return(HttpNotFound("Budget not found.")); } var category = ctx.Categories.Find(model.CategoryId); if (category == null) { return(HttpNotFound("Category not found.")); } if (!ModelState.IsValid) { return(View(model)); } var service = CreateBudgetCategoryService(); if (service.CreateBudgetCategory(model)) { TempData["SaveResult"] = "Your Budget Category was created."; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Budget Category could not be created."); return(View(model)); }
public bool CreateBudgetCategory(BudgetCategoryCreate model) { var entity = new BudgetCategory() { BudgetId = model.BudgetId, CategoryId = model.CategoryId, Amount = model.Amount }; using (var ctx = new ApplicationDbContext()) { ctx.BudgetCategory.Add(entity); return(ctx.SaveChanges() == 1); } }