public ActionResult Edit([Bind(Include = "Id,Created,Updated,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 (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; } db.Entry(blogPost).State = EntityState.Modified; blogPost.Slug = Slug; blogPost.Updated = DateTimeOffset.Now; db.SaveChanges(); return(RedirectToAction("Details", "BlogPosts", new { Slug = Slug })); } return(View(blogPost)); }
public ActionResult Edit([Bind(Include = "Id,PostId,AuthorId,Body,Created,Updated,UpdateReason")] Comment comment) { if (ModelState.IsValid) { comment.AuthorId = User.Identity.GetUserId(); comment.Updated = DateTimeOffset.Now; db.Entry(comment).State = EntityState.Modified; 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)); }