title: Binding MVC4 to An Object Containing Arrays
body: <p>I am creating a form to allow employees to submit timesheets online. The form consists of a number of sections, each representing a day of the week. Within each section, the user can add multiple rows, each row reflecting a task undertaken that day.</p>
  
  <pre><code>function AddRow(day)
  {
      var count = $("#RowCount_" + day).data("count");
  
      var row = "&lt;tr&gt;" +
                     "&lt;td&gt;&amp;nbsp;&lt;/td&gt;" +
                      "&lt;td&gt;&lt;input type='text' size='50' id='" + day + "[" + count + "].Description' name='timesheet." + day + "[" + count + "].Description' class='Description' data-day='" + day + "' /&gt;&lt;/td&gt;" +
                      "&lt;td&gt;&lt;input type='text' size='6' id='" + day + "[" + count + "].Start' name='timesheet." + day + "[" + count + "].Start' /&gt;&lt;/td&gt;" +
                      "&lt;td&gt;&lt;input type='text' size='6' id='" + day + "[" + count + "].Finish' name='timesheet." + day + "[" + count + "].Finish' /&gt;&lt;/td&gt;" +
                      "&lt;td&gt;&lt;input type='text' size='6' id='" + day + "[" + count + "].Travel' name='timesheet." + day + "[" + count + "].Travel'/&gt;&lt;/td&gt;" +
                      "&lt;td&gt;" +
                          "&lt;select style='width:200px' id='" + day + "[" + count + "].JobNo' name='timesheet." + day + "[" + count + "].JobNo'&gt;@Html.Raw(ViewBag.Jobs)&lt;/select&gt;" +
                      "&lt;/td&gt;" +
                      "&lt;td&gt;&lt;input type='text' size='6' id='" + day + "[" + count + "].Hrs' name='timesheet." + day + "[" + count + "].Hrs' /&gt;&lt;/td&gt;" +
                  "&lt;/tr&gt;";
  
      $("#Table_" + day).append(row);
      $("#RowCount_" + day).data("count", ++count);
  }
  </code></pre>
  
  <p>I then want to bind this to a <code>Timesheet</code> object, as follows:</p>
  
  <pre><code>public class Timesheet
  {
      List&lt;Core.Models.TimesheetEntry&gt; Monday = new List&lt;TimesheetEntry&gt;();
      TimesheetEntry[] Tuesday { get; set; }
      TimesheetEntry[] Wednesday { get; set; }
      TimesheetEntry[] Thursday { get; set; }
      TimesheetEntry[] Friday { get; set; }
      TimesheetEntry[] Saturday { get; set; }
      TimesheetEntry[] Sunday { get; set; }
  }
  </code></pre>
  
  <p>(note I am trying both lists and arrays here, neither of which bind, but more of this in a minute)</p>
  
  <p>The <code>TimesheetEntry</code> is as follows:</p>
  
  <pre><code>public class TimesheetEntry
  {
      public string Description { get; set; }
      public string Start { get; set; }
      public string Finish { get; set; }
      public decimal Travel { get; set; }
      public string JobNo { get; set; }
      public decimal Hrs { get; set; }
  }
  </code></pre>
  
  <p>This posts to the following controller method:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Create(Core.Models.Timesheet timesheet)
  {
      return View();
  }
  </code></pre>
  
  <p>When submitted, the form does not bind (all fields in timesheet are 0 or null). However, if I change the controller to </p>
  
  <pre><code>public ActionResult Create(List&lt;Core.Models.TimesheetEntry&gt; Monday)
  {
      return View();
  }
  </code></pre>
  
  <p>and adjust the name of the html fields to <code>name='" + day + "[" + count + "].Description'</code> then it will bind just fine. This is a little messy, so I'd rather use the <code>Timesheet</code> class if possible. Is there an issue binding to Lists within models?</p>
  
  <p>EDIT: Below is a snippet of the post data, with a single entry and 2 blank entries for monday, 3 blank for tuesday:</p>
  
  <pre><code>timesheet.Monday[0].Description:test entry
  timesheet.Monday[0].Start:0900
  timesheet.Monday[0].Finish:1700
  timesheet.Monday[0].Travel:0
  timesheet.Monday[0].JobNo:14089A - line 14 hut heater sockets
  timesheet.Monday[0].Hrs:7.5
  timesheet.Monday[1].Description:
  timesheet.Monday[1].Start:
  timesheet.Monday[1].Finish:
  timesheet.Monday[1].Travel:
  timesheet.Monday[1].JobNo:14089A - line 14 hut heater sockets
  timesheet.Monday[1].Hrs:
  timesheet.Monday[2].Description:
  timesheet.Monday[2].Start:
  timesheet.Monday[2].Finish:
  timesheet.Monday[2].Travel:
  timesheet.Monday[2].JobNo:14089A - line 14 hut heater sockets
  timesheet.Monday[2].Hrs:
  timesheet.Tuesday[0].Description:
  timesheet.Tuesday[0].Start:
  timesheet.Tuesday[0].Finish:
  timesheet.Tuesday[0].Travel:
  timesheet.Tuesday[0].JobNo:14089A - line 14 hut heater sockets
  timesheet.Tuesday[0].Hrs:
  timesheet.Tuesday[1].Description:
  timesheet.Tuesday[1].Start:
  timesheet.Tuesday[1].Finish:
  timesheet.Tuesday[1].Travel:
  timesheet.Tuesday[1].JobNo:14089A - line 14 hut heater sockets
  timesheet.Tuesday[1].Hrs:
  </code></pre>