public void SaveComment(Comment comment) { using (var context = new MyContext()) { if (comment.CommentId == 0) { context.Users.Attach(comment.UserPosted); context.Posts.Attach(comment.Post); context.Comments.Add(comment); } else { context.Comments.Attach(comment); context.Entry(comment).State = EntityState.Modified; } context.SaveChanges(); } }
public ActionResult Create(CommentInfo commentInfo) { if (ModelState.IsValid) { var comment = new Comment(); comment.Date = DateTime.Now; comment.Text = commentInfo.Text; comment.Post = new Post() { PostId = commentInfo.PostId}; comment.UserPosted = CurrentUser; if (commentInfo.CommentId != 0) comment.ToComment = new Comment { CommentId = commentInfo.CommentId }; repository.SaveComment(comment); return RedirectToAction("ShowPost", "Post", new { postId = commentInfo.PostId}); } return View(commentInfo); }