public void InsertOrUpdate(Category category) { if (category.Id == default(int)) { // New entity context.Categories.Add(category); } else { // Existing entity context.Entry(category).State = EntityState.Modified; } }
public ActionResult Create(Category category) { if (ModelState.IsValid) { categoryRepository.InsertOrUpdate(category); categoryRepository.Save(); return RedirectToAction("Index", new RouteValueDictionary {{"departmentId", category.DepartmentId}}); } Department dept = departmentRepository.Find(category.DepartmentId); category.DepartmentId = dept.Id; category.Department = dept; PopulateDropDownList(); return View(category); }
private Category LoadCategory(int departmentId) { Department dept = departmentRepository.Find(departmentId); var category = new Category {DepartmentId = dept.Id, Department = dept}; PopulateDropDownList(); return category; }
public ActionResult Edit(Category category) { if (ModelState.IsValid) { categoryRepository.InsertOrUpdate(category); categoryRepository.Save(); return RedirectToAction("Index", new {departmentId = category.DepartmentId}); } PopulateDropDownList(); return View(category); }