title: How to do Lists in a view model? + dataannotations
body: <p>I am using asp.net mvc 3, data annotations and auto mapper.</p>
  
  <p>I want to have all my annotations on properties in my view model once the properties pass validation I use auto mapper to map it back to my domain object.</p>
  
  <p>I have a viewmodel that has properties that I want to have a collection of since I want to generate a table from them. I also want to use them later to use as a form to add rows to this table.</p>
  
  <p>So what do I do? How do I take these properties and make a collection of them?</p>
  
  <pre><code>public class UserViewModel()
  {
      [Required()]
      public string UserName = {get; set;}
      [Required()]
      public string FirstName = {get; set;}
      [Required()]
      public string LastName = {get; set;}
  }
  </code></pre>
  
  <p>I want to use these properties to generate my table and be used for a form.</p>
  
  <p>The only thing I can think of is doing this</p>
  
  <pre><code>   public class AddUserViewModel()
      {
          [Required()]
          public string UserName = {get; set;}
          [Required()]
          public string FirstName = {get; set;}
          [Required()]
          public string LastName = {get; set;}
      }
  
     public class UserViewModel()
     {
         public List&lt;User&gt; Users {get; set;}
         public AddUserViewModel {get; set;}
  
         public UserViewModel()
         {
             Users = new List&lt;Users&gt;();
         }
     }
  </code></pre>
  
  <p>So basically I have it as a separate view model that is enclosed into another viewmodel that contains a list of Users(my domain model)</p>
  
  <p>That way I use my domain model to generate the table and my AddUserViewModel for my adding of users.</p>
  
  <p>Seems kinda redundant so I am not sure if there is a better way.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I have something like this</p>
  
  <pre><code> var viewModel = new UserViewModel();
   List&lt;Users&gt; users=  UserService.GetAllUsers();
   viewModel = Mapper.Map&lt;Users, UserViewModel&gt;(users);
   return View("Index", viewModel);
  </code></pre>
  
  <p>I also tried</p>
  
  <pre><code>var viewModel = new UserViewModel();
   List&lt;Users&gt; users=  UserService.GetAllUsers();
   viewModel.AddUserViewModel = Mapper.Map&lt;Users, AddUserViewModel&gt;(users);
   return View("Index", viewModel);
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p>I have this and it compiles but I get this error</p>
  
  <pre><code>        SomeViewModel viewModel = new SomeViewModel ();       
          List&lt;User&gt; users=  userService.GetAllUsers();
          viewModel.UserViewModel = Mapper.Map&lt;List&lt;User&gt;, List&lt;UserViewModel&gt;&gt;(users);
          return View("Index", viewModel);
  
  
  Trying to map Domain.User to ViewModels.UserViewModel.
  Missing type map configuration or unsupported mapping.
  Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
  </code></pre>