public ActionResult EditComment(int?id) { if (!id.HasValue) { return(RedirectToAction(nameof(HomeController.Index))); } var comment = DbContext.Comments.FirstOrDefault( p => p.Id == id); if (comment == null) { return(RedirectToAction(nameof(HomeController.Index))); } var model = new CommentHomeViewModel(); model.Body = comment.Body; model.BlogPostId = comment.BlogPostId; model.ModifyingReason = comment.ModifyingReason; model.UserId = comment.UserId; model.Id = comment.Id; model.DateUpdated = DateTimeOffset.Now; return(View("CommentEdit", model)); }
public ActionResult EditComment(int?id, CommentHomeViewModel model) { if (!ModelState.IsValid) { return(View("_Comment", model)); } Comment comment; if (!id.HasValue) { comment = new Comment(); DbContext.Comments.Add(comment); } else { comment = DbContext.Comments.FirstOrDefault(p => p.Id == id); if (comment == null) { return(RedirectToAction(nameof(HomeController.Index))); } } comment.Body = model.Body; comment.DateUpdated = model.DateUpdated; comment.ModifyingReason = model.ModifyingReason; DbContext.SaveChanges(); return(RedirectToAction(nameof(HomeController.Details), new { slug = DbContext.BlogPosts.FirstOrDefault(p => p.Id == comment.BlogPostId).Slug })); }
public ActionResult Comment(int?id, CommentHomeViewModel model) { if (!id.HasValue || !ModelState.IsValid) { return(RedirectToAction(nameof(HomeController.Index))); } Comment comment = new Comment { Body = model.Body, UserId = User.Identity.GetUserId(), BlogPostId = id.Value }; BlogPost post = DbContext.BlogPosts.FirstOrDefault(p => p.Id == id.Value); post.Comments.Add(comment); DbContext.SaveChanges(); return(RedirectToAction(nameof(HomeController.Details), new { slug = post.Slug })); }