public Post CreatePost(Post post) { if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.BadRequest); } db.Posts.Add(post); db.SaveChanges(); return(post); }
public ActionResult Create([Bind(Include = "PostId,PostHeading,PostContent")] Post post) { post.UserId = User.Identity.GetUserId(); post.PostDate = DateTime.Now; ModelState.Clear(); TryValidateModel(post); if (ModelState.IsValid) { db.Posts.Add(post); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(post)); }
public ActionResult Create([Bind(Include = "CommentId,CommentContent,PostId")] Comment comment) { comment.UserId = User.Identity.GetUserId(); comment.CommentDate = DateTime.Now; if (ModelState.IsValid) { db.Comments.Add(comment); db.SaveChanges(); return(RedirectToAction("Details", "Posts", new { id = comment.PostId })); } ViewBag.PostId = comment.PostId; return(View(comment)); }