public ActionResult EditPage(int id) { //Declare PageVM PageVM model; //Get the page using (DataContext db = new DataContext()) { //Select the row from the value in 'id' PageDTOes dto = db.Pages.Find(id); //Confirm if page exists if (dto == null) { return(Content("The page does not exist.")); } //Initialize ViewModel model = new PageVM(dto); } //Return view with Model return(View(model)); }
public ActionResult EditPage(PageVM model) { //Check if model state is valuid if (!ModelState.IsValid) { return(View(model)); } using (DataContext db = new DataContext()) { //Get page ID int id = model.Id; //Initialize slug string slug = "home"; //Get the page PageDTOes dto = db.Pages.Find(id); //DTO the title dto.Title = model.Title; //Check slug and 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(); } } //Make sure title and slug are unique if (db.Pages.Where(p => p.Id != id).Any(p => p.Title == model.Title) || db.Pages.Where(p => p.Id != id).Any(p => p.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"] = "Successfully edited the page!"; //Redirect to action return(RedirectToAction("EditPage")); }
public PageVM(PageDTOes row) { Id = row.Id; Title = row.Title; Slug = row.Slug; Body = row.Body; Sorting = row.Sorting; HasSidebar = row.HasSidebar; }
public ActionResult AddPage(PageVM model) { //Check modelstate if (!ModelState.IsValid) { return(View(model)); } using (DataContext db = new DataContext()) { //Declare slug string slug; //Initialize PageDTOes PageDTOes dto = new PageDTOes(); //DTOes Title dto.Title = model.Title; //Check and set Slug if (string.IsNullOrWhiteSpace(model.Slug)) { slug = model.Title.Replace(" ", "-").ToLower(); } else { slug = model.Slug.Replace(" ", "-").ToLower(); } //Make title and slug are unique if (db.Pages.Any(p => p.Title == model.Title) || db.Pages.Any(p => p.Slug == slug)) { ModelState.AddModelError("", "That title or slug already exists."); return(View(model)); } //DTO the rest dto.Slug = slug; dto.Body = model.Body; dto.HasSidebar = model.HasSidebar; dto.Sorting = 100; //Save the DTO db.Pages.Add(dto); db.SaveChanges(); } //Set tempData message TempData["SM"] = "You have added a new page"; //Redirect return(RedirectToAction("AddPage")); }
public ActionResult DeletePage(int id) { using (DataContext db = new DataContext()) { //Get the page PageDTOes dto = db.Pages.Find(id); //Remove the page db.Pages.Remove(dto); //Save db.SaveChanges(); } //Redirect return(RedirectToAction("Index")); }
public ActionResult DetailsPage(int id) { //Declare PageVM PageVM model; using (DataContext db = new DataContext()) { //get the page PageDTOes dto = db.Pages.Find(id); //Confirm page if it exists if (dto == null) { return(Content("The page does not exist.")); } model = new PageVM(dto); } return(View(model)); }