public ActionResult Create(CategoryModel model) { if (ModelState.IsValid) { model.Url = model.Url.ReplaceWhiteSpacesWithHyphens().ToLower(); if (!Context.Categories.Any(c => c.Url.ToLower() == model.Url.ToLower())) { Category newCategory = new Category { CreateDate = DateTime.Now }; model.UpdateCategory(newCategory); Context.Categories.Add(newCategory); Context.SaveChanges(); return RedirectToAction("AllCategories"); } ModelState.AddModelError("Name", Config.TextErrorMessage); } return View(model); }
public ActionResult Edit(CategoryModel model, String returnUrl) { Category oldCategory = Context.Categories. SingleOrDefault(c => c.Id == model.Id); if (oldCategory == null) { return RedirectToRoute("HttpError404"); } if (ModelState.IsValid) { oldCategory.Name = model.Name; oldCategory.IsPublic = model.IsPublic; Context.SaveChanges(); if (Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } return RedirectToAction("AllCategories"); } model.Url = oldCategory.Url; return View(model); }