how can i have this model in the view
public class PostDetailsViewModel
{
public Post Posts1 { get; set; }
public IEnumerable<Post> Posts2 { get; set; }
}
view
@model IEnumerable<forum3.ViewModels.PostDetailsViewModel>
@foreach (var item in Model)
{
<span>@item.Posts1.Title</span><br />
<p>@item.Posts1.Question</p><br /><br />
}
controller
public ViewResult PostDetails(int id)
{
PostDetailsViewModel postdetailsviewmodel = new PostDetailsViewModel();
postdetailsviewmodel.Posts1 = postRepository.Find(id);
postdetailsviewmodel.Posts2 = postRepository.FindPostByParentIds(id);
List<PostDetailsViewModel> postDetailsList = new List<PostDetailsViewModel>();
postDetailsList.Add(postdetailsviewmodel);
return View(postDetailsList);
}
how can i have post2 in the view which are the answers
Assuming you want to show a list of questions and its answers and have your View model like this.
( I changed Post1 to Question and Posts2 to Answers for better readability)
Assuming your controller returns a List of PostDetailsViewModel and PostDetailsViewModel has a Question Property of Type Post and Answers Property of type List
This View will give you the output
EDIT : After seeing your controller action method, i guess you want to show a specific post ( Question ) and its answers. So Just return one PostViewModel object from the action to your view instead of returning a List of them
And change your View like this