public ActionResult Index(int id, int?Comment) { string id_user = User.Identity.GetUserId(); _viewModel = new PostViewModel(); CreateLayoutView("Trả lời câu hỏi"); Post post = _postService.GetById(id); ApplicationUser user = _service.GetUserById(post.Id_User); FieldHelper.CopyNotNullValue(PostViewModel, user); FieldHelper.CopyNotNullValue(PostViewModel, post); PostViewModel.TypeToString = _postTypeService.GetById(post.Id_Type).Name; PostVoteDetail votePost = _postVoteDetailService.getVoteByIdUser(id_user, post.Id); CommentViewModel commentCorrected = null; if (votePost != null) { PostViewModel.Vote = votePost.Vote; } List <Comment> listComment = new List <Comment>(); List <Comment> listChildComment = new List <Comment>(); listComment = _commentOfPost.getCommentOfPost(post.Id); foreach (var parent in listComment) { CommentViewModel commentViewModel = new CommentViewModel(); user = _service.GetUserById(parent.Id_User); CommentVoteDetail voteComment = _commentVoteDetailService.getVoteByIdUser(id_user, parent.Id); if (voteComment != null) { commentViewModel.Vote = voteComment.Vote; } FieldHelper.CopyNotNullValue(commentViewModel, user); FieldHelper.CopyNotNullValue(commentViewModel, parent); listChildComment = _commentOfPost.getChildOfComment(parent.Id_Post, parent.Id); if (listChildComment.Count != 0) { foreach (var child in listChildComment) { CommentViewModel commentChildViewModel = new CommentViewModel(); user = _service.GetUserById(child.Id_User); FieldHelper.CopyNotNullValue(commentChildViewModel, user); FieldHelper.CopyNotNullValue(commentChildViewModel, child); commentViewModel.listChildComment.Add(commentChildViewModel); } } if (Comment != null && commentViewModel.Id.Equals(Comment)) { PostViewModel.listComment.Insert(0, commentViewModel); } else { if (commentViewModel.Corrected) { commentCorrected = commentViewModel; } else { PostViewModel.listComment.Add(commentViewModel); } } } if (commentCorrected != null) { PostViewModel.listComment.Insert(0, commentCorrected); } List <PostType> listPostType = _postTypeService.GetAll().ToList(); List <PostTypeViewModel> listPostTypeViewModel = new List <PostTypeViewModel>(); foreach (PostType item in listPostType) { PostTypeViewModel postType = new PostTypeViewModel(); FieldHelper.CopyNotNullValue(postType, item); postType.Register = _detailUserTypeService.getRegisterPostType(id_user, item.Id); listPostTypeViewModel.Add(postType); } ViewBag.ListPostType = listPostTypeViewModel; return(View(PostViewModel)); }
public ActionResult Index(PostViewModel data, string userType, int?type) { _viewModel = new HomeViewModel(); string id_user = User.Identity.GetUserId(); List <Post> listPost = new List <Post>(); if (type == null) { CreateLayoutView("Trang chủ"); listPost = _postService.getPostByUserType(id_user).Take(5).ToList(); } else { CreateLayoutView("Thể loại: " + _postTypeService.GetById(type.GetValueOrDefault()).Name); listPost = _postService.getPostByType(type.GetValueOrDefault()).Take(5).ToList(); } List <PostType> listPostType = _postTypeService.GetAll().ToList(); foreach (PostType item in listPostType) { PostTypeViewModel postType = new PostTypeViewModel(); FieldHelper.CopyNotNullValue(postType, item); postType.Register = _detailUserTypeService.getRegisterPostType(id_user, item.Id); HomeViewModel.ListPostType.Add(postType); } ViewBag.newMember = userType; ViewBag.type = type.GetValueOrDefault(); foreach (Post post in listPost) { PostViewModel postViewModel = new PostViewModel(); ApplicationUser user = _service.GetUserById(post.Id_User); FieldHelper.CopyNotNullValue(postViewModel, user); FieldHelper.CopyNotNullValue(postViewModel, post); postViewModel.TypeToString = _postTypeService.GetById(post.Id_Type).Name; PostVoteDetail votePost = _postVoteDetailService.getVoteByIdUser(id_user, post.Id); if (votePost != null) { postViewModel.Vote = votePost.Vote; } List <Comment> listComment = new List <Comment>(); List <Comment> listChildComment = new List <Comment>(); listComment = _commentOfPost.getCommentOfPost(post.Id); foreach (var parent in listComment) { CommentViewModel commentViewModel = new CommentViewModel(); user = _service.GetUserById(parent.Id_User); CommentVoteDetail voteComment = _commentVoteDetailService.getVoteByIdUser(id_user, parent.Id); if (voteComment != null) { commentViewModel.Vote = voteComment.Vote; } FieldHelper.CopyNotNullValue(commentViewModel, user); FieldHelper.CopyNotNullValue(commentViewModel, parent); listChildComment = _commentOfPost.getChildOfComment(parent.Id_Post, parent.Id); if (listChildComment.Count != 0) { foreach (var child in listChildComment) { CommentViewModel commentChildViewModel = new CommentViewModel(); user = _service.GetUserById(child.Id_User); FieldHelper.CopyNotNullValue(commentChildViewModel, user); FieldHelper.CopyNotNullValue(commentChildViewModel, child); commentViewModel.listChildComment.Add(commentChildViewModel); } } if (commentViewModel.Corrected) { postViewModel.listComment.Insert(0, commentViewModel); } else { postViewModel.listComment.Add(commentViewModel); } } HomeViewModel.ListPost.Add(postViewModel); } return(View(HomeViewModel)); }