Beispiel #1
0
 // GET: /Admin/Pages/Create
 public ActionResult Create()
 {
     var ViewModel = new Create();
     ViewModel.GetLayouts();
     ViewModel.GetParents(db);
     return View(ViewModel);
 }
        public ActionResult Create(Create viewModel)
        {
            // TODO Ensure slug isn't equal to the name of any controllers, or does not start with <controller>/something

            if (ModelState.IsValid)
            {
                string RequestDomain = Globals.GetRequestDomain();

                // Ensure slug is unique
                string Slug = Globals.GetSlug(viewModel.Slug, true);
                if (db.SitePages.Any(x => (x.Site.Domain == RequestDomain) && (x.Slug == Slug)))
                {
                    ModelState.AddModelError("SlugAlreadyUsed", "Slug has already been used");
                    viewModel.GetParents(db);
                    return View(viewModel);
                }
                else
                {
                    SitePage NewPage = ModelConverter.Convert<SitePage>(viewModel);

                    // Assign values for fields not on form
                    NewPage.DateAdded = DateTime.Now;
                    NewPage.DateLastUpdated = DateTime.Now;
                    NewPage.SiteId = db.Sites.Single(x => x.Domain == RequestDomain).Id;

                    // Transform values
                    NewPage.Html = Globals.SaveImagesToDisk(NewPage.Html);
                    if (NewPage.ParentId <= 0) NewPage.ParentId = null;
                    NewPage.Slug = Globals.GetSlug(NewPage.Slug, true);

                    // Save changes
                    db.SitePages.Add(NewPage);
                    db.SaveChanges();

                    // Update cache
                    Caching.ResetPages();

                    return RedirectToAction("Index");
                }
            }
            else
            {
                viewModel.GetParents(db);
                return View(viewModel);
            }
        }
Beispiel #3
0
        public ActionResult Create(Create viewModel)
        {
            if (ModelState.IsValid)
            {
                string RequestDomain = Globals.GetRequestDomain(ControllerContext.RequestContext.HttpContext);

                // Ensure slug is unique
                string Slug = Globals.GetSlug(viewModel.Slug, true);
                if (db.SitePages.Any(x => (x.Site.Domain == RequestDomain) && (x.Slug == Slug)))
                {
                    ModelState.AddModelError("SlugAlreadyUsed", "Slug has already been used");
                    viewModel.GetLayouts();
                    viewModel.GetParents(db);
                    return View(viewModel);
                }
                else
                {
                    SitePage NewPage = ModelConverter.Convert<SitePage>(viewModel);

                    // Assign values for fields not on form
                    NewPage.DateAdded = DateTime.Now;
                    NewPage.DateLastUpdated = DateTime.Now;
                    NewPage.SiteId = db.Sites.Single(x => x.Domain == RequestDomain).Id;

                    // Transform values
                    NewPage.Slug = Globals.GetSlug(NewPage.Slug, true);
                    NewPage.Html = Globals.SaveImagesToDisk(NewPage.Html, ControllerContext.HttpContext);

                    // Save changes
                    db.SitePages.Add(NewPage);
                    db.SaveChanges();

                    // Update cache
                    DatabaseCache.ResetNavMenuItems(ControllerContext.RequestContext.HttpContext);

                    return RedirectToAction("Index");
                }
            }
            else
            {
                viewModel.GetLayouts();
                viewModel.GetParents(db);
                return View(viewModel);
            }
        }