コード例 #1
0
        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"));
        }
コード例 #2
0
        // GET: Admin/Pages
        public ActionResult Index()
        {
            //Declare list of PageVM
            List <PageVM> listPage;

            using (DbShoppingCard db = new DbShoppingCard())
            {
                listPage = db.Pages.ToArray().OrderBy(x => x.Sorting).Select(x => new PageVM(x)).ToList();
            }
            //Init the list

            //Return view with list
            return(View(listPage));
        }
コード例 #3
0
        // GET: Admin/Shop/Categories
        public ActionResult Categories()
        {
            //Declare list of model
            List <CategoriesVM> listCategoriesVM;

            using (DbShoppingCard db = new DbShoppingCard())
            {
                //Init the list
                listCategoriesVM = db.Categories.ToArray().OrderBy(x => x.Sorting).Select(x => new CategoriesVM(x)).ToList();
            }

            //Return view with list
            return(View(listCategoriesVM));
        }
コード例 #4
0
        // 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"));
        }
コード例 #5
0
        public ActionResult EditPage(int id)
        {
            //Declare pageVM
            PageVM pageVM;

            using (DbShoppingCard db = new DbShoppingCard())
            {
                //Get the page
                PageDTO dto = db.Pages.Find(id);
                //Confirm page exists
                if (dto == null)
                {
                    return(Content("The page does not exists"));
                }
                //init pageVM
                pageVM = new PageVM(dto);
            }

            //Return view with model
            return(View(pageVM));
        }
コード例 #6
0
        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"));
        }