Пример #1
0
        public ActionResult Create([Bind(Include = "Id,Title,Body,MediaURL,Published")] BlogPost.Models.BlogPost blogPost, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                var Slug = StringUtilites.URLFriendly(blogPost.Title);
                if (String.IsNullOrWhiteSpace(Slug))
                {
                    ModelState.AddModelError("Title", "Invalid title");
                    return(View(blogPost));
                }
                if (ImageUploadValidator.IsWebFriendlyImage(image))
                {
                    var fileName = Path.GetFileName(image.FileName);
                    image.SaveAs(Path.Combine(Server.MapPath("~/Uploads/"), fileName));
                    blogPost.MediaUrl = "/Uploads/" + fileName;
                }

                if (db.Posts.Any(p => p.Slug == Slug))
                {
                    ModelState.AddModelError("Title", "The title must be unique");
                    return(View(blogPost));
                }

                blogPost.Slug    = Slug;
                blogPost.Created = DateTimeOffset.Now;
                db.Posts.Add(blogPost);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(blogPost));
        }
Пример #2
0
        public ActionResult Edit([Bind(Include = "Id,Created,Title,Body,Slug,MediaURL,Published,Abstract")] BlogPost blogPost, HttpPostedFileBase image)
        {
            var Slug = StringUtilites.URLFriendly(blogPost.Title);

            blogPost.AuthorId = User.Identity.GetUserId();

            if (String.IsNullOrWhiteSpace(Slug))
            {
                ModelState.AddModelError("Title", "Invalid title");
                return(View(blogPost));
            }
            if (Slug != blogPost.Slug && db.BlogPosts.Any(p => p.Slug == Slug))
            {
                ModelState.AddModelError("Title", "The title must be unique");
                return(View(blogPost));
            }

            if (ImageUploadValidator.IsWebFriendlyImage(image))
            {
                var filename = Path.GetFileName(image.FileName);
                image.SaveAs(Path.Combine(Server.MapPath("~/Uploads/"), filename));
                blogPost.MediaURL = "/Uploads/" + filename;
            }
            else
            {
                blogPost.MediaURL = blogPost.MediaURL;
            }
            if (ModelState.IsValid)
            {
                //blogPost.Slug = Slug;
                db.Entry(blogPost).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(blogPost));
        }