public ActionResult Topic(int Id) { //.Include(x => x.Posts) var topic = _Context.Topics.Single(x => x.Id == Id); var posts = GetPosts(topic).ToList(); //foreach (var item in GetPosts(topic)) //{ // //.Include(x => x.User).Include(x=>x.Replies) // posts.Add(_Context.Posts.SingleOrDefault(x => x.Id == item.Id)); //} var postsVM = new List <PostListingViewModel>(); //foreach (var x in posts) //{ // var postListing = new PostListingViewModel // { // Id = x.Id, // AuthorId = x.User.Id, // AuthorName = x.User.UserName, // Title = x.Title, // DatePosted = x.Created, // TopicId = x.Topic.Id, // Topic = BuildTopicListing(x), // RepliesCount = x.Replies.Count() // }; // postsVM.Add(postListing); //} var postListings = posts.Select(x => new PostListingViewModel { Id = x.Id, AuthorId = x.User.Id, AuthorName = x.User.UserName, Title = x.Title, DatePosted = x.Created, TopicId = x.Topic.Id, Topic = BuildTopicListing(x), RepliesCount = _Context.PostReplies.Where(r => r.Post.Id == x.Id).Count() }).ToList(); var model = new TopicPostsViewModel { Posts = postListings, Topic = BuildTopicListing(topic) }; return(View(model)); }
public ActionResult Posts(int id) { var posts = unitOfWork.SubTopicRepository.GetPosts(id).ToList(); var name = unitOfWork.SubTopicRepository.GetSubtopicById(id).SubtopicName; TopicPostsViewModel model = new TopicPostsViewModel { Posts = posts, TopicName = name, Topics = unitOfWork.MainTopicRepository.GetTopics().ToList() }; return(View(model)); }