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); } }