public CommentViewModel CreateComment(int answerId, string comment) { var answer = _context.Answers.FirstOrDefault(ans => ans.AnswerId == answerId && ans.Active); if (answer == null) { return null; } using (var transaction = _context.Database.BeginTransaction()) { var user = GetCurrentUser(); var commentEntity = answer.AddComment(user, comment); try { _context.SaveChanges(); commentEntity.AddNotifications(); _context.SaveChanges(); transaction.Commit(); var model = new CommentViewModel() { AnswerId = answerId, AvatarUrl = GetAvatarUrl(user), Comment = commentEntity, CommentParagraphs = comment.SplitLines(), ScreenName = GetUserScreenName(user.UserName), UserName = user.UserName, }; return model; } catch (Exception e) { return null; } } }
public ActionResult PostCommentForm(AnswerProfileViewModel model) { var comment = new CommentViewModel() { AnswerId = model.Answer.AnswerId }; return PartialView("CommentPost", comment); }