public ActionResult Create(Category category) { var existing = db.Categories.Where(x=>x.Name== category.Name).FirstOrDefault(); if (existing != null) { existing.IsDeleted = false; db.SaveChanges(); } else { if (ModelState.IsValid) { var user = GetUser(); category.IsDeleted = false; category.User = user; db.Categories.Add(category); db.SaveChanges(); } } return RedirectToAction("Index"); }
private Category LoadOrCreateCategory(string categoryName) { var user = GetUser(); var existing = db.Categories.Where(x => x.Name == categoryName).FirstOrDefault(); if (existing == null) { var newCategory = new Category() { IsDeleted = false, Name = categoryName, User = user }; db.SaveChanges(); return newCategory; } else { return existing; } }
public ActionResult Edit(Category category) { if (ModelState.IsValid) { db.Entry(category).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(category); }