public ActionResult EditPage(PageViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (DataB db = new DataB()) { //Get page id int id = model.Id; //initialize string slug = "home"; //get the page PageDtO dto = db.pages.Find(id); //dto the title dto.Title = model.Title; //check for 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 //x => x.Id != id is more or less equivalent to //SomeFunction (x) { return x.Id != id; } if (db.pages.Where(x => x.Id != id).Any(x => x.Title == model.Title) || db.pages.Where(x => x.Id != id).Any(x => x.Slug == slug)) { ModelState.AddModelError("", "That title or slug already exists."); return(View(model)); } //DTO the slug body and sidebar dto.Slug = slug; dto.Body = model.Body; dto.HasSideBar = model.HasSideBar; //Save db.SaveChanges(); } TempData["SM"] = "You have edited the page!"; //redirect return(RedirectToAction("EditPage")); }
public PageViewModel(PageDtO row) { Id = row.Id; Title = row.Title; Slug = row.Slug; Body = row.Body; Sort = row.Sort; HasSideBar = row.HasSideBar; }
public ActionResult AddPage(PageViewModel model) { //check model state if (!ModelState.IsValid) { return(View(model)); } using (DataB db = new DataB()) { //declare the slug string slug; //init page DtO PageDtO dto = new PageDtO(); //DtO Title 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 on /Admin/Pages/AddPage //notice a a model is returned in View() //thus the fields will remain filled out if (db.pages.Any(x => x.Title == model.Title) || db.pages.Any(x => x.Slug == slug)) { ModelState.AddModelError("", "That title or slug already exists."); return(View(model)); } //Save DtO db.pages.Add(dto); db.SaveChanges(); } //tempdata persists after redirection and viewback TempData["SM"] = "Successfully added a new page!!!"; //Redirect return(RedirectToAction("AddPage")); }
//GET: Admin/Pages/DeletePage/id public ActionResult DeletePage(int id) { using (DataB db = new DataB()) { //Get page PageDtO dto = db.pages.Find(id); //Remove page db.pages.Remove(dto); //Save db.SaveChanges(); //redirect return(RedirectToAction("Index")); } }
//GET: Admin/Pages/PageDetails/id public ActionResult PageDetails(int id) { PageViewModel model; using (DataB db = new DataB()) { //Get the page PageDtO dto = db.pages.Find(id); //confirm page exists if (dto == null) { return(Content("The Page doesn't exist!!!")); } //initialize the pageviewmodel by 1 parameter constructor model = new PageViewModel(dto); } return(View(model)); }
public ActionResult EditPage(int id) { PageViewModel model; using (DataB db = new DataB()) { //Get the page PageDtO dto = db.pages.Find(id); //confirm that the page exists if (dto == null) { return(Content("The page does not exist")); } //no need to initialize all fields because //of the handy 1 parameter constructor in the //PageViewModel Class! model = new PageViewModel(dto); } return(View(model)); }