title: Creating View Models consisting of objects of other models in MVC3
body: <p>I have a model and an enumerable of that model that I am putting together into a view model in asp.net mvc3 like so.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.ComponentModel.DataAnnotations;
  
  
  namespace FuelPortal.Models.DBModels
  {
      public class UserDBModel
      {
  
          public int userId { get; set; }
  
          [Required, Display(Name = "User Name")]
          public string userName { get; set; }
  
          [Required, Display(Name = "Password")]        
          public string password { get; set; }
  
          [Required, Display(Name = "First Name")]
          public string firstName { get; set; }
  
          [Required, Display(Name = "Last Name")]
          public string lastName { get; set; }
  
          [Required, Display(Name = "Email")]
          public string email { get; set; }
  
          [Display(Name = "Phone")]
          public string phone { get; set; }
  
          [Display(Name = "Account Expiration Date(Optional)")]
          public DateTime expireDate { get; set; }
  
          [Display(Name = "Password Expiration Date")]
          public DateTime passwordExpireDate { get; set; }
  
          [Display(Name = "Last Date Modified")]
          public DateTime DateModified { get; set; }
  
      }
  }
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using FuelPortal.Models.DBModels;
  
  namespace FuelPortal.Models.ViewModels
  {
      public class UserMaintenanceViewModel
      {
  
          public UserDBModel user = new UserDBModel();
          public List&lt;UserDBModel&gt; userList = new List&lt;UserDBModel&gt;();
      }
  }
  </code></pre>
  
  <p>Now I strongly type a view to use the view model.</p>
  
  <pre><code>@model FuelPortal.Models.ViewModels.UserMaintenanceViewModel  
  &lt;div class="frmBG"&gt;
          @using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "wrapperData" }))
          {
  
                  &lt;h5&gt;Create New User&lt;/h5&gt; 
                  &lt;br /&gt;
                  &lt;table cellpadding="2" cellspacing="10" border="0"&gt;
                  &lt;tr&gt;&lt;td&gt;
                  &lt;div class="editor-label"&gt;
                      @Html.LabelFor(model =&gt; model.user.userName)
                  &lt;/div&gt;&lt;/td&gt;&lt;td&gt;
  
                  &lt;div class="editor-field"&gt;
                      @Html.EditorFor(model =&gt; model.user.userName)
                      @Html.ValidationMessageFor(model =&gt; model.user.userName)
                  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
  
                  &lt;tr&gt;&lt;td&gt;
                  &lt;div class="editor-label"&gt;
                      @Html.LabelFor(model =&gt; model.user.password)
                  &lt;/div&gt;&lt;/td&gt;&lt;td&gt;
                  &lt;div class="editor-field"&gt;
                      @Html.PasswordFor(model =&gt; model.user.password)
                      @Html.ValidationMessageFor(model =&gt; model.user.password)
                  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
  
                  &lt;tr&gt;&lt;td&gt;
                  &lt;div class="editor-label"&gt;
                      @Html.LabelFor(model =&gt; model.user.firstName)
                  &lt;/div&gt;&lt;/td&gt;&lt;td&gt;
                  &lt;div class="editor-field"&gt;
                      @Html.EditorFor(model =&gt; model.user.firstName)
                      @Html.ValidationMessageFor(model =&gt; model.user.firstName)
                  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
  
                  &lt;tr&gt;&lt;td&gt;
                  &lt;div class="editor-label"&gt;
                      @Html.LabelFor(model =&gt; model.user.lastName)
                  &lt;/div&gt;&lt;/td&gt;&lt;td&gt;
                  &lt;div class="editor-field"&gt;
                      @Html.EditorFor(model =&gt; model.user.lastName)
                      @Html.ValidationMessageFor(model =&gt; model.user.lastName)
                  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
  
                  &lt;tr&gt;&lt;td&gt;
                  &lt;div class="editor-label"&gt;
                      @Html.LabelFor(model =&gt; model.user.email)
                  &lt;/div&gt;&lt;/td&gt;&lt;td&gt;
                  &lt;div class="editor-field"&gt;
                      @Html.EditorFor(model =&gt; model.user.email)
                      @Html.ValidationMessageFor(model =&gt; model.user.email)
                  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
  
                  &lt;tr&gt;&lt;td&gt;
                  &lt;div class="editor-label"&gt;
                      @Html.LabelFor(model =&gt; model.user.phone)
                  &lt;/div&gt;&lt;/td&gt;&lt;td&gt;
                  &lt;div class="editor-field"&gt;
                      @Html.EditorFor(model =&gt; model.user.phone)
                      @Html.ValidationMessageFor(model =&gt; model.user.phone)
                  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
  
                  &lt;tr&gt;&lt;td&gt;
                  &lt;div class="editor-label"&gt;
                      @Html.LabelFor(model =&gt; model.user.expireDate)
                  &lt;/div&gt;&lt;/td&gt;&lt;td&gt;
                  &lt;div class="editor-field"&gt;
                      @Html.EditorFor(model =&gt; model.user.expireDate)
                      @Html.ValidationMessageFor(model =&gt; model.user.expireDate)
                  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
  
                  &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;
                  &lt;p&gt;
                      &lt;input type="submit" value="" class="create" /&gt;&lt;input type="button" value="" class="close_tab"/&gt;
                  &lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;
                  &lt;/table&gt;
          }
      &lt;/div&gt;
  </code></pre>
  
  <p>For some reason on post it does not populate the object within the view model. So when i create a post action all values of (UserMaintenanceViewModel  model) are null. Like so..</p>
  
  <pre><code> [HttpPost]
          public ActionResult CreateUser(UserMaintenanceViewModel model)
          {
              try
              {
                  /// TODO: Add insert logic here
                  var result = repository.insertNewUser(Session["UserId"].ToString(), model.user.UserName, model.user.Password, model.user.FirstName,
                                                        model.user.LastName, model.user.Email, model.user.Phone, model.user.ExpireDate);
                  // If result &gt; 0 user insert successful
  </code></pre>
  
  <p>Also, if I switch to getting postback through a (FormCollection form) I see that the names of the html elements are "user.UserName" instead of the expected "UserName". Any guidance would be greatly appreciated I feel like I should be able to accomplish this.</p>
  
  <p>Thanks so much</p>