public ActionResult AddComment(int problemId, string commentText) { if (!User.Identity.IsAuthenticated) return RedirectToAction("Login", "Account"); if (string.IsNullOrWhiteSpace(commentText)) return Json(new { commentHtml = "" }); commentRepository.Insert(new Comment { dateTime = DateTime.Now, ProblemId = problemId, Text = commentText, UserId = UserManager.FindByName(User.Identity.Name).Id }); var cvm = new CommentViewModel() { AddingTime = DateTime.Now, AuthorName = User.Identity.Name, ProblemId = problemId, Text = commentText }; return Json(new { html = RenderPartialViewToString("_CommentView", cvm) }); }
public static ShowProblemViewModel GetShowProblemViewModel(Problem problem) { var spvm = new ShowProblemViewModel(); spvm.Author = problem.Author.UserName; spvm.Name = problem.Name; spvm.Text = problem.Text; spvm.Id = problem.Id; spvm.Category = problem.Category.Name; spvm.IsBlocked = problem.IsBlocked; var sb = new StringBuilder(); foreach (var tag in problem.Tags) { sb.Append(tag.Name); sb.Append(','); } spvm.TagsString = sb.ToString(); sb.Clear(); foreach (var ans in problem.CorrectAnswers) { sb.Append(ans.Text); sb.Append(';'); } sb.Remove(sb.Length - 1, 1); spvm.Answers = sb.ToString(); spvm.Comments = new List<CommentViewModel>(); foreach (var comment in problem.Comments) { CommentViewModel com = new CommentViewModel() { AddingTime = comment.dateTime, AuthorName = comment.User.UserName, ProblemId = problem.Id, Text = comment.Text }; spvm.Comments.Add(com); } spvm.Likes = problem.Likes.Count; spvm.Dislikes = problem.Dislikes.Count; return spvm; }
//[HttpPost] //public ActionResult InfiniteScroll(int? taskId, int blockNumber) //{ // System.Threading.Thread.Sleep(1000); // const int blockSize = 5; // var comments = GetTasksComments(taskId, blockNumber); // var jsonModel = new JsonModel // { // NoMoreData = comments.Count < blockSize, // HtmlString = RenderPartialViewToString("_CommentViewPartial", comments), // }; // return Json(jsonModel); //} private string RenderPartialViewToString(string viewName, CommentViewModel comment) { if (string.IsNullOrEmpty(viewName)) { viewName = ControllerContext.RouteData.GetRequiredString("action"); } ViewData.Model = comment; using (var sw = new StringWriter()) { var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); viewResult.View.Render(viewContext, sw); return sw.GetStringBuilder().ToString(); } }