public pageVM(pageDTO row) { Id = row.Id; Title = row.Title; Slug = row.Slug; Body = row.Body; Sorting = row.Sorting; HasSidebar = row.HasSidebar; }
public ActionResult EditPage(PageVM model) { //check model state if (!ModelState.IsValid) { return(View(model)); } using (Db db = new Db()) { //get page id int id = model.Id; //Intitialize slug string slug = "home"; //get the page pageDTO dto = db.Pages.Find(id); //DTO 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 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 Exist."); 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["Success Message"] = "You Have Edit The Page!"; //Redirect return(RedirectToAction("EditPage")); }
// GET: Admin/Pages/DeletePage/id public ActionResult DeletePage(int id) { using (Db db = new Db()) { //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) { //Get model state if (!ModelState.IsValid) { return(View(model)); } using (Db db = new Db()) { //Get Id int Id = model.Id; //Declare Slug string slug = "home"; //Get the page pageDTO dto = db.Pages.Find(Id); //DTO title dto.Title = model.Title; //Check slug and set it if needed if (model.Slug != "home") { if (string.IsNullOrWhiteSpace(model.Slug)) { slug = model.Title.Replace(" ", "-").ToLower(); } else { slug = model.Slug.Replace(" ", "-").ToLower(); } } //Check if slug and title unique 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("", "The title or slug already exists"); return(View(model)); } //dto the rest dto.Slug = slug; dto.Body = model.Body; dto.HasSidebar = model.HasSidebar; //save dto db.SaveChanges(); } //save tempdata message TempData["SM"] = "The page has been edited!!!"; //Redirect return(RedirectToAction("EditPage")); }
// GET: Admin/Pages/editpage/id public ActionResult EditPage(int id) { PageVM model; using (var Db = new Db()) { pageDTO dto = Db.Pages.Find(id); //get data to dto frm DB if (dto == null) { return(HttpNotFound()); } model = new PageVM(dto); //load dto data to pageVM } return(View(model)); }
public ActionResult EditPage(PageVM model) { if (!ModelState.IsValid) { return(View(model)); } int id = model.Id; string slug = "home"; using (var db = new Db()) { //find the data pageDTO dto = db.Pages.Find(id); dto.Title = model.Title; if (model.Slug != "home") { if (string.IsNullOrWhiteSpace(model.Slug)) { slug = model.Title.Replace(" ", "-").ToLower(); } else { slug = model.Slug.Replace(" ", "-").ToLower(); } } //chk sure title and slug is unique 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 is already exist !"); return(View(model)); } //set dto dto.Slug = slug; dto.Body = model.Body; dto.HasSidebar = model.HasSidebar; //update db.SaveChanges(); } //set msg TempData["SM"] = "You have edited a page !"; //redirect return(RedirectToAction("AddPage")); }
public ActionResult AddPage(PageVM model) { //check model state if (!ModelState.IsValid) { return(View(model)); } using (Db db = new Db()) { //declare slug string slug; //Initialize pageDTO pageDTO dto = new pageDTO(); //DTO Title dto.Title = model.Title; //check forand 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(x => x.Title == model.Title) || db.Pages.Any(x => x.Slug == model.Slug)) { ModelState.AddModelError("", "That Title Or Slug Already Exist."); 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["Success Message"] = "You Have Added A New Page!"; //Redirect return(RedirectToAction("AddPage")); }
// GET: Admin/Pages/Delete/id public ActionResult Delete(int id) { using (var db = new Db()) { //find the data pageDTO dto = db.Pages.Find(id); if (dto == null) { return(HttpNotFound()); } //remove db.Pages.Remove(dto); db.SaveChanges(); } //redirect return(RedirectToAction("Index")); }
public ActionResult AddPage(pageVM model) { //Check model state if (!ModelState.IsValid) { return(View(model)); } using (Db db = new Db()) { //Declare Slug string slug; //Init pageDTO pageDTO dto = new pageDTO(); //DTO title dto.Title = model.Title; //Check for and Set Slug if needed if (string.IsNullOrWhiteSpace(model.Slug)) { slug = model.Title.Replace(" ", "-").ToLower(); } else { slug = model.Slug.Replace(" ", "-").ToLower(); } //Making sure title & slug unique if (db.Pages.Any(x => x.Title == model.Title) || db.Pages.Any(x => x.Slug == model.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; dto.Sorting = 100; //Save DTO db.Pages.Add(dto); db.SaveChanges(); } //Save TempData Message TempData["SM"] = "Page has been added!"; //Redirect return(RedirectToAction("AddPage")); }
public ActionResult EditPage(int id) { //Declare PageVM PageVM model; using (Db db = new Db()) { //Get The Page pageDTO dto = db.Pages.Find(id); //confirm page exist if (dto == null) { return(Content("The Page Doesn't Exist.")); } //initialize pageVM model = new PageVM(dto); } //return view with model return(View(model)); }
public ActionResult AddPage(PageVM model) { if (!ModelState.IsValid) { return(View(model)); } using (var db = new Db()) { string slug; var dto = new pageDTO(); dto.Title = model.Title; if (string.IsNullOrWhiteSpace(model.Slug)) { slug = model.Title.Replace(" ", "-").ToLower(); } else { slug = model.Slug.Replace(" ", "-").ToLower(); } //chk sure title and slug is unique if (db.Pages.Any(x => x.Title == model.Title) || db.Pages.Any(x => x.Slug == model.Slug)) { ModelState.AddModelError("", "that title or slug is already exist"); return(View(model)); } //set dto dto.Slug = slug; dto.Body = model.Body; dto.HasSidebar = model.HasSidebar; dto.Sorting = 100; //save db.Pages.Add(dto); db.SaveChanges(); } //set msg TempData["SM"] = "You have added a new page !"; //redirect return(RedirectToAction("AddPage")); }
public ActionResult EditPage(int id) { //Declare PageVM pageVM model; using (Db db = new Db()) { //Get Page pageDTO dto = db.Pages.Find(id); //Confirm Page exist if (dto == null) { return(Content("The Page does not exist")); } //Init PageVM model = new pageVM(dto); } //Return View With Model return(View(model)); }
//POST: Admin/Pages/PageDetails public ActionResult PageDetails(int id) { //Declare PageVM pageVM model; using (Db db = new Db()) { //Get the Page pageDTO dto = db.Pages.Find(id); //Confirm Page Exists if (dto == null) { return(Content("The Page doesn't exist")); } model = new pageVM(dto); //Init PageVM } //Return View with model return(View(model)); }