コード例 #1
0
        public ActionResult Create(Page page, HttpPostedFileBase socialImage)
        {
            if (ModelState.IsValid)
            {
                page.LastUpdated = DateTime.Now;

                // perfor some basic verification that it is an int
                int subPageId = Convert.ToInt32(page.SubPageToPageId);

                // set the order by counting the current pages
                // also need to check if it's a main page or a sub page
                if (subPageId == 0)
                {
                    // update the order for the remaining pages
                    // this is because the order is not correct otherwise
                    var pages = context.Pages.Where(a => a.SubPageToPageId == 0).OrderBy(a => a.Order).ToList();
                    for (int i = 0; i < pages.Count; i++)
                    {
                        pages[i].Order = i + 1;
                    }

                    context.SaveChanges();

                    page.Order = context.Pages.Where(a => a.SubPageToPageId == 0).ToList().Count + 1;
                }
                else
                {
                    // update the order for the remaining pages
                    // this is because the order is not correct otherwise
                    var pages = context.Pages.Where(a => a.SubPageToPageId == subPageId).OrderBy(a => a.Order).ToList();
                    for (int i = 0; i < pages.Count; i++)
                    {
                        pages[i].Order = i + 1;
                    }

                    context.SaveChanges();

                    page.Order = context.Pages.Where(a => a.SubPageToPageId == subPageId).ToList().Count + 1;
                }

                // allowed file extensions
                string[] fileExt = { ".png", ".jpg", ".gif", ".jpeg" };

                // save the social media image
                if (socialImage != null && socialImage.ContentLength > 0)
                {
                    try
                    {
                        // make sure that the file extension is among the allowed
                        if (Array.IndexOf(fileExt, Path.GetExtension(socialImage.FileName.ToLower())) < 0)
                        {
                            throw new Exception("Social media image was in an unsupported format. Only images (JPEG, GIF, PNG) allowed.");
                        }

                        var path = Path.Combine(Server.MapPath("~/Images/PageImages"), CustomHelpers.RemoveSwedishChars(socialImage.FileName));
                        page.SocialMediaImage = CustomHelpers.RemoveSwedishChars(socialImage.FileName);
                        socialImage.SaveAs(path);
                    }
                    catch (Exception e)
                    {
                        TempData["ErrorMessage"] = e.Message;
                    }
                }

                context.Pages.Add(page);
                context.SaveChanges();

                // display a friendly success message
                TempData["StatusMessage"] = "Page created successfully!";

                // if the user added a special page, redirect back to index
                // this is because there isn't really much to continue editing except the title
                if (!String.IsNullOrEmpty(page.CustomPage))
                {
                    return(RedirectToAction("Index"));
                }

                // the user added a blank page, therefore we redirect back to the edit page
                return(RedirectToAction("Edit", new { id = page.PageId }));
            }

            return(View(page));
        }
コード例 #2
0
        public ActionResult Edit(Page page, HttpPostedFileBase socialImage)
        {
            var path = Server.MapPath("~/Images/PageImages");

            if (ModelState.IsValid)
            {
                page.LastUpdated = DateTime.Now;

                // allowed file extensions
                string[] fileExt = { ".png", ".jpg", ".gif", ".jpeg" };

                // save the social media image
                if (socialImage != null && socialImage.ContentLength > 0)
                {
                    try
                    {
                        // make sure that the file extension is among the allowed
                        if (Array.IndexOf(fileExt, Path.GetExtension(socialImage.FileName.ToLower())) < 0)
                        {
                            throw new Exception("Social media image was in an unsupported format. Only images (JPEG, GIF, PNG) allowed.");
                        }

                        // delete the current image
                        if (!String.IsNullOrEmpty(page.SocialMediaImage))
                        {
                            if (System.IO.File.Exists(Path.Combine(path, page.SocialMediaImage)))
                            {
                                System.IO.File.Delete(Path.Combine(path, page.SocialMediaImage));
                            }
                        }


                        page.SocialMediaImage = CustomHelpers.RemoveSwedishChars(socialImage.FileName);
                        socialImage.SaveAs(Path.Combine(Server.MapPath("~/Images/PageImages"), CustomHelpers.RemoveSwedishChars(socialImage.FileName)));
                    }
                    catch (Exception e)
                    {
                        TempData["ErrorMessage"] = e.Message;
                    }
                }

                // update the page
                context.Entry(page).State = EntityState.Modified;
                context.SaveChanges();

                // display a friendly success message
                TempData["StatusMessage"] = "The changes were saved successfully!";

                // if the user added a special page, redirect back to index
                // this is because there isn't really much to continue editing except the title
                if (!String.IsNullOrEmpty(page.CustomPage))
                {
                    return(RedirectToAction("Index"));
                }

                // redirect to the same edit page in case the user wants to make more changes
                if (!String.IsNullOrEmpty(Request.QueryString["scripts"]))
                {
                    return(RedirectToAction("Edit", new { id = page.PageId, scripts = "true" }));
                }

                return(RedirectToAction("Edit", new { id = page.PageId }));
            }


            // ModelState not valid
            return(View(page));
        }