title: How to add a list to a View Model MVC
body: <p>I'm using MVC 3 with View Model, in my case I have a View Model that should display a list of items and also a form for inserting some input.</p>
  
  <p>I have problem in my View because I'm not able to associate the Form for inserting the data with the view model, could you tell me what I'm doing wrong?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.ComponentModel.DataAnnotations;
  using TestGuestBook.Models;
  
  namespace TestGuestBook.ViewModel
  {
      public class ListAddCommentsViewModel
      {
          public int CommentId { get; set; }
  
          [Required]
          public string Nominative { get; set; }
  
          [Email]
          public string Email { get; set; }
  
          [Required]
          public string Content { get; set; }
  
          public List&lt;Comment&gt; CommentItems { get; set; }
      }
  }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>@model IEnumerable&lt;TestGuestBook.ViewModel.ListAddCommentsViewModel&gt;
  
  @{
      ViewBag.Title = "Index";
  }
  
  &lt;h2&gt;Index&lt;/h2&gt;
  
  @using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
          &lt;legend&gt;ListAddCommentsViewModel&lt;/legend&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.CommentId)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.CommentId)
              @Html.ValidationMessageFor(model =&gt; model.CommentId)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Nominative)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Nominative)
              @Html.ValidationMessageFor(model =&gt; model.Nominative)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Email)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Email)
              @Html.ValidationMessageFor(model =&gt; model.Email)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Content)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Content)
              @Html.ValidationMessageFor(model =&gt; model.Content)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Create" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  
  &lt;p&gt;
      @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;
              CommentId
          &lt;/th&gt;
          &lt;th&gt;
              Nominative
          &lt;/th&gt;
          &lt;th&gt;
              Email
          &lt;/th&gt;
          &lt;th&gt;
              Content
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
  @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.CommentId)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Nominative)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Email)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Content)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
              @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
              @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  
  &lt;/table&gt;
  </code></pre>