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) { if (ModelState.IsValid) { string RequestDomain = Globals.GetRequestDomain(ControllerContext.RequestContext.HttpContext); SiteBlogPost EditedPost = db.SiteBlogPosts.SingleOrDefault(x => (x.Id == viewModel.Id) && (x.Site.Domain == RequestDomain)); if (EditedPost == null) { return(HttpNotFound()); } else { // View model to domain model ModelConverter.Convert(viewModel, EditedPost); // Assign values for fields not on form EditedPost.DateLastUpdated = DateTime.Now; // Transform values if (string.IsNullOrWhiteSpace(EditedPost.Slug)) { EditedPost.Slug = EditedPost.Title; } EditedPost.Slug = Globals.GetSlug(EditedPost.Slug, false); // No need to enforce uniqueness, since slug isn't actually used for lookup EditedPost.FullPostText = Globals.SaveImagesToDisk(EditedPost.FullPostText, ControllerContext.HttpContext); EditedPost.PreviewText = Globals.SaveImagesToDisk(EditedPost.PreviewText, ControllerContext.HttpContext); // Save changes db.Entry(EditedPost).State = EntityState.Modified; db.SaveChanges(); // Update cache DatabaseCache.RemoveBlogDetails(viewModel.Id, ControllerContext.RequestContext.HttpContext); DatabaseCache.ResetBlogIndex(ControllerContext.RequestContext.HttpContext); return(RedirectToAction("Index")); } } else { return(View(viewModel)); } }