title: Best practice for following the view model pattern
body: <p>I'm learning ASP.NET MVC for some times now. I follow some guidelines found on internet or in books and I would like to be sure I follow good practices in my developments regarding the view model pattern. Below is a simple example of implementation of a blog. Could you please confirm me that I'm on the right way. Let's say I would like to display a post title + description on a view and below that comments on this post. So I have a view with 2 partial views on it.</p>
  
  <p><strong>In my controller:</strong></p>
  
  <pre><code>    public ActionResult DetailPost(int postID)
      {
          // retrieve infos
          Post postModel = repository.Posts.FirstOrDefault(p =&gt; p.PostID == postID);
          IEnumerable&lt;Comments&gt; commentsModel = postModel.Comments;
  
          // prepare view model
          DetailPostViewModel detailPostViewModel = new DetailPostViewModel
                                                          {
                                                              Post = postModel,
                                                              Comments = commentsModel,
                                                          };
  
          return View(detailPostViewModel);
      }
  </code></pre>
  
  <p>So here, I perpare a view model composed of 2 things: </p>
  
  <ul>
  <li>the post informations </li>
  <li>an enumeration of comments. </li>
  </ul>
  
  <p>The view model will be passed to the view for following the view model pattern.</p>
  
  <p><strong>In my Detail view:</strong></p>
  
  <pre><code>@model WebUI.ViewModels.DetailPostViewModel
  
  @{
      ViewBag.Title = "Detail";
  }
  
  &lt;h2&gt;Detail&lt;/h2&gt;
  
  @Html.Partial("_DetailPost", Model.Post)
  @Html.Partial("_Comments",  Model.Comments)
  </code></pre>
  
  <p>So here, I used my DetailPostViewModel. For my partial views, I passed the necessary information which are entities!? I don't know if this is the right way or do I have to pass exclusively view models here too? </p>
  
  <p><strong>In my _DetailPost view:</strong></p>
  
  <pre><code>@model Domain.Entities.Post
  
  @Html.DisplayForModel()
  ...
  </code></pre>
  
  <p>So here in my partial view, I used en entity as model. Is it good or bad?</p>
  
  <p><strong>In my _Comments view:</strong></p>
  
  <pre><code>@model IEnumerable&lt;Domain.Entities.Comment&gt;
  
  @foreach (var item in Model)
  {
      @Html.DisplayFor(Model =&gt; item.Title)
      @Html.DisplayFor(Model =&gt; item.Username)
  }
  </code></pre>
  
  <p>So here in my partial view, I used en entity as model. Is it good or bad?</p>
  
  <p>Thanks for your advises/suggestions.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I try to implement my view model pattern with the help of Automapper but I have some problems as described below. I understand the principe of mapping  one object to another. In a basic situation it works very well. Now, let's see the a more complex situation: I have a post composed of some attributes (id, title, ...) AND each post can have some comments (object) attached AND each comment can have only one user (object) attached. I would like to get a view model with all my posts and for each one, get all the comments and for each comment get the user attached.</p>
  
  <p>Here are the domain entities:</p>
  
  <pre><code>public class Post
  {
      public int    PostID    { get; set; }
      public string Title     { get; set; }
      ...
      public virtual ICollection&lt;Comment&gt; Comments { get; set; }
  }
  
  public class Comment
  {
      public int    CommentID   { get; set; }
      public string CommentText { get; set; }
      ...
      public virtual &lt;User&gt; User { get; set; }
  }
  
  public class User
  {
      public int UserID      { get; set; }
      public string Username { get; set; }
      ...
  }
  </code></pre>
  
  <p>Here are the view models:</p>
  
  <pre><code>public class PostVM
  {
      public int    PostID    { get; set; }
      public string Title     { get; set; }
      ...
      public virtual ICollection&lt;CommentVM&gt; CommentsVM { get; set; }
  }
  
  public class CommentVM
  {
      public int    CommentID   { get; set; }
      public string CommentText { get; set; }
      ...
      public virtual &lt;UserVM&gt; UserVM { get; set; }
  }
  
  public class UserVM
  {
      public int UserID      { get; set; }
      public string Username { get; set; }
      ...
  }
  </code></pre>
  
  <p>Here is my controller:</p>
  
  <pre><code>// I would like to list all posts in the DB.
  public ViewResult Index()
  {
      PostVM viewModel = new PostVM 
                             {
                                 ...
                             }
  
      return View(viewModel);
  }
  </code></pre>
  
  <p>How do I map my list of (entity) posts objects into my view model object and all sub objects (comments, users)?</p>
  
  <p>Thanks!</p>