0

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

3
  • Please post the code for your controller action. Commented Apr 19, 2012 at 18:22
  • 3
    Assuming the code shown is what you're using, nothing should be stopping you from writing @item.Posts2.Count() for example. Commented Apr 19, 2012 at 18:23
  • well the thing is that i have made all the fetches in the controller now i want to just display the fetches . Commented Apr 19, 2012 at 18:27

1 Answer 1

1

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)

public class PostDetailsViewModel
{
    public Post Question { get; set; }
    public List<Post> Answers { get; set; }
}
public class Post
{
    public int ID { set; get; }
    public string Title { set; get; }
}

Assuming your controller returns a List of PostDetailsViewModel and PostDetailsViewModel has a Question Property of Type Post and Answers Property of type List

public ActionResult Posts()
{
  List<PostDetailsViewModel> objVMLsit = new List<PostDetailsViewModel>();
  //add the object to objVMList here   

  return View(objVMLsit);
}

This View will give you the output

@model List<MvcApplication1.Models.PostDetailsViewModel>
@foreach (var item in Model)
{
  <h2>@item.Question.Title</h2><br />  
  foreach (var answer in item.Answers)
  {
        <p>@answer.Title</p>
  }   
}

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

public ViewResult PostDetails(int id)
{
    PostDetailsViewModel postdetailsviewmodel = new PostDetailsViewModel();
    postdetailsviewmodel.Question= postRepository.Find(id);
    postdetailsviewmodel.Answers= postRepository.FindPostByParentIds(id);
    return View(postdetailsviewmodel);
}

And change your View like this

@model MvcApplication1.Models.PostDetailsViewModel
<h2>@Model.Question.Title</h2><br />  
@foreach (var answer in Model.Answers)
{
   <p>@answer.Title</p>
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.