public ActionResult AddPage(PageVM model) { //Check model state if (!ModelState.IsValid) { return(View(model)); } using (DbShoppingCard db = new DbShoppingCard()) { //Declare slug string slug; //Init pageDTO PageDTO dto = new PageDTO(); //DTO title //Model binding in action dto.Title = model.Title; //Check for and set slug if need be if (string.IsNullOrWhiteSpace(model.Slug)) { slug = model.Title.Replace(" ", "-").ToLower(); } else { slug = model.Slug.Replace(" ", "-").ToLower(); } //Make sure title and slug are unique if (db.Pages.Any(t => t.Title == model.Title) || db.Pages.Any(s => s.Slug == slug)) { ModelState.AddModelError("", "Title or Slug already exists"); return(View(model)); } //DTO the rest dto.Slug = slug; dto.Body = model.Body; dto.HasSideBar = model.HasSideBar; dto.Sorting = 50; //Save DTO db.Pages.Add(dto); db.SaveChanges(); } //Set TempData message TempData["SM"] = "You have added new page"; //Redirect return(RedirectToAction("AddPage")); }
// GET: Admin/Pages/DeletePage/id public ActionResult DeletePage(int id) { using (DbShoppingCard db = new DbShoppingCard()) { //Get the page PageDTO dto = db.Pages.Find(id); //Remove the page db.Pages.Remove(dto); //Save db.SaveChanges(); } //Redirect return(RedirectToAction("Index")); }
public ActionResult EditPage(PageVM model) { //Check model state if (!ModelState.IsValid) { return(View(model)); } using (DbShoppingCard db = new DbShoppingCard()) { //Get page id int id = model.Id; //Init slug string slug = "home"; //Get the page PageDTO dto = db.Pages.Find(id); //DTO the title dto.Title = model.Title; //Check for the slug set it if need be if (model.Slug != "home") { if (string.IsNullOrWhiteSpace(model.Slug)) { slug = model.Title.Replace(" ", "-").ToLower(); } else { slug = model.Slug.Replace(" ", "-").ToLower(); } } //Check sure title and slug are unique //Lambda expresion if (db.Pages.Where(x => x.Id != id).Any(t => t.Title == model.Title) || (db.Pages.Where(x => x.Id != id).Any(s => s.Slug == slug))) { ModelState.AddModelError("", "The title or slug already exists"); return(View(model)); } //DTO the rest dto.Slug = slug; dto.Body = model.Body; dto.HasSideBar = model.HasSideBar; //Save the DTO db.SaveChanges(); } //Set TempData message TempData["SM"] = "You have edited the page"; //Redirect view with Edit Page return(RedirectToAction("Index")); }