Exemplo n.º 1
0
        public ActionResult Create([Bind(Include = "Title,Slug,Body,MediaURL,Published,Abstract")] BlogPost blogPost, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                var Slug = StringUtilities.URLFriendly(blogPost.Title);
                if (String.IsNullOrWhiteSpace(Slug))
                {
                    ModelState.AddModelError("Title", "Invalid title");
                    return(View(blogPost));
                }
                if (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;
                }

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

            return(View(blogPost));
        }
Exemplo n.º 2
0
        public ActionResult Create([Bind(Include = "Id,PostId,AuthorId,Body,Created,Updated,UpdateReason")] Comment comment)
        {
            if (ModelState.IsValid)
            {
                comment.AuthorId = User.Identity.GetUserId();
                comment.Created  = DateTimeOffset.Now;
                db.Comments.Add(comment);
                db.SaveChanges();
                var slug = db.BlogPosts.Find(comment.PostId).Slug;
                return(RedirectToAction("Details", "BlogPosts", new { Slug = slug }));
            }

            ViewBag.AuthorId = new SelectList(db.Users, "Id", "FirstName", comment.AuthorId);
            ViewBag.PostId   = new SelectList(db.BlogPosts, "Id", "Title", comment.PostId);
            return(View(comment));
        }