public ActionResult Edit(Edit viewModel) { if (ModelState.IsValid) { string RequestDomain = Globals.GetRequestDomain(ControllerContext.RequestContext.HttpContext); SitePage EditedPage = db.SitePages.SingleOrDefault(x => (x.Id == viewModel.Id) && (x.Site.Domain == RequestDomain)); if (EditedPage == null) { return HttpNotFound(); } else { // Ensure slug is unique string OldSlug = EditedPage.Slug; string NewSlug = Globals.GetSlug(viewModel.Slug, true); if ((OldSlug != NewSlug) && (db.SitePages.Any(x => (x.Site.Domain == RequestDomain) && (x.Slug == NewSlug)))) { ModelState.AddModelError("SlugAlreadyUsed", "Slug has already been used"); viewModel.GetLayouts(); viewModel.GetParents(db); return View(viewModel); } else { // View model to domain model ModelConverter.Convert(viewModel, EditedPage); // Assign values for fields not on form EditedPage.DateLastUpdated = DateTime.Now; // Transform values EditedPage.Slug = NewSlug; EditedPage.Html = Globals.SaveImagesToDisk(EditedPage.Html, ControllerContext.HttpContext); // Save changes db.Entry(EditedPage).State = EntityState.Modified; db.SaveChanges(); // Update cache DatabaseCache.RemoveSitePage(ControllerContext.RequestContext.HttpContext, OldSlug); DatabaseCache.RemoveSitePage(ControllerContext.RequestContext.HttpContext, NewSlug); DatabaseCache.ResetNavMenuItems(ControllerContext.RequestContext.HttpContext); return RedirectToAction("Index"); } } } else { viewModel.GetLayouts(); viewModel.GetParents(db); return View(viewModel); } }
public ActionResult Edit(Edit viewModel) { // TODO Ensure slug isn't equal to the name of any controllers, or does not start with <controller>/something if (ModelState.IsValid) { string RequestDomain = Globals.GetRequestDomain(); SitePage EditedPage = db.SitePages.SingleOrDefault(x => (x.Id == viewModel.Id) && (x.Site.Domain == RequestDomain)); if (EditedPage == null) { return HttpNotFound(); } else { // Ensure slug is unique string OldSlug = EditedPage.Slug; string NewSlug = Globals.GetSlug(viewModel.Slug, true); if ((OldSlug != NewSlug) && (db.SitePages.Any(x => (x.Site.Domain == RequestDomain) && (x.Slug == NewSlug)))) { ModelState.AddModelError("SlugAlreadyUsed", "Slug has already been used"); viewModel.GetParents(db); return View(viewModel); } else { // View model to domain model ModelConverter.Convert(viewModel, EditedPage); // Assign values for fields not on form EditedPage.DateLastUpdated = DateTime.Now; // Transform values EditedPage.Html = Globals.SaveImagesToDisk(EditedPage.Html); if (EditedPage.ParentId <= 0) EditedPage.ParentId = null; EditedPage.Slug = NewSlug; // Save changes db.Entry(EditedPage).State = EntityState.Modified; db.SaveChanges(); // Update cache Caching.ResetPages(); return RedirectToAction("Index"); } } } else { viewModel.GetParents(db); return View(viewModel); } }