title: How to bind a nested model in MVC3?
body: <p>I've been reading all about UpdateModel() and custom model binders and i still cant figure this out. Seems like theres gotta be a simple answer to this.</p>
  
  <p>I have a class called user that i user all over my MVC Web app.</p>
  
  <pre><code> public class User
  {
      [Key]
      public int ID { get; set; }
  
      [Required]
      public string Name { get; set; }
  
      [Required]
      [Email]
      public string Email { get; set; }
  
      [Required]
      public string Password { get; set; }
  
      public virtual ICollection&lt;User&gt; WorkTasks { get; set; }
  }
  </code></pre>
  
  <p>And then a WorkTask that has it in a few places:</p>
  
  <pre><code>public class WorkTask
  {
      [Key]
      public int ID { get; set; }
      [Required]
      public string Name { get; set; }
      public  DateTime? DesiredStartDate { get; set; }
      public  DateTime? DesiredEndDate { get; set; }
  
  
      public TimeSpan? DesiredTimeSpent { get; set; }
  
      public virtual ICollection&lt;WorkTaskTag&gt; Tags { get; set; }
      public virtual ICollection&lt;WorkTaskPeriod&gt; Periods { get; set; }
  
      public virtual ICollection&lt;User&gt; InvolvedUsers { get; set; }
      public virtual User CreatedBy { get; set; }
      public virtual User AssignedTo { get; set; }
  
      public string UserNameAssignedTo
      {
          get
          {
              if(AssignedTo!=null)
              return AssignedTo.Name;
              return CreatedBy.Name;
          }
      }
  
      public string TotalTimeSpent { 
          get
          {
              var concretePeriods = Periods
                  .Where(i =&gt; i.StartDate.HasValue &amp;&amp; i.EndDate.HasValue);
              if (concretePeriods != null &amp;&amp; concretePeriods.Count() &gt; 0)
              {
                  TimeSpan ts = new TimeSpan();
                  foreach (var p in concretePeriods)
                  {
                      var t=p.EndDate.Value-p.StartDate.Value;
                      ts.Add(t);
                  }
                  TimePeriodHelpers help = new TimePeriodHelpers();
                  return help.GetTimeFormat(ts);
              }
              return "0:00";
          }
      }
  }
  </code></pre>
  
  <p>So how do i make a create template for this WorkTask that allows the User class to be bound to the WorkTask in multiple places?</p>
  
  <p>Here's my very shoddy attempt:</p>
  
  <pre><code>[HttpPost]
      public ActionResult Create(WorkTask worktask)
      {
          LoadUsers();
          string assignedto=Request["AssignedTo"];
          var user = db.Users.First(i =&gt; SqlFunctions.StringConvert((double)i.ID) == assignedto);
          UpdateModel&lt;User&gt;(user);
          if (ModelState.IsValid)
          {
              db.WorkTasks.Add(worktask);
              db.SaveChanges();
              return RedirectToAction("Index");  
          }
  
          return View(worktask);
      }
  </code></pre>
  
  <p>and the view:</p>
  
  <pre><code>@using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  &lt;fieldset&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.Name)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.Name)
          @Html.ValidationMessageFor(model =&gt; model.Name)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.DesiredStartDate,"Desired Start Date")
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.DesiredStartDate)
          @Html.ValidationMessageFor(model =&gt; model.DesiredStartDate)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.DesiredEndDate,"Desired End Date")
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.DesiredEndDate)
          @Html.ValidationMessageFor(model =&gt; model.DesiredEndDate)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.DesiredTimeSpent,"Desired Time Spent")
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.DesiredTimeSpent)
          @Html.ValidationMessageFor(model =&gt; model.DesiredTimeSpent)
      &lt;/div&gt;
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.AssignedTo,"User Assigned To")
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.DropDownListFor(i=&gt;Model.AssignedTo,(IEnumerable&lt;SelectListItem&gt;)ViewBag.Users)
          @Html.ValidationMessageFor(model =&gt; model.AssignedTo)
      &lt;/div&gt;
      &lt;p&gt;
          &lt;input type="submit" value="Create" /&gt;
      &lt;/p&gt;
  </code></pre>