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 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"));
        }
        public ActionResult EditSideBar(SideBarViewModel model)
        {
            using (DataB db = new DataB())
            {
                SideBarDtO dto = db.sidebar.Find(1);

                dto.Body = model.Body;

                db.SaveChanges();
            }

            TempData["SM"] = "You made a successful edit to the sidebar!!!";

            return(RedirectToAction("EditSidebar"));
        }
        //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"));
            }
        }
        public void ReorderPages(int[] id)
        {
            using (DataB db = new DataB())
            {
                //Set initial count
                int count = 1;

                //Declare PageDtO
                PageDtO dto;

                //Set sorting for each page
                foreach (var pageId in id)
                {
                    dto      = db.pages.Find(pageId);
                    dto.Sort = count;

                    db.SaveChanges();

                    count++;
                }
            }
        }