title: ASP.NET Web Forms 4.5 model binding where the model contains a collection
body: <p>I'm trying to update an old Web Forms application to use the new model binding features added in 4.5, similar to the MVC binding features.</p>
  
  <p>I'm having trouble making an editable FormView that presents a single model that contains simple members plus a member that is a collection of other models. I need the user to be able to edit the simple properties of the parent object and the properties of the child collection.</p>
  
  <p>The problem is that the child collection (<code>ProductChoice.Extras</code>) is always null after model binding when the code is trying to update the model.</p>
  
  <p>Here are my models:</p>
  
  <pre><code>[Serializable]
  public class ProductChoice
  {
      public ProductChoice()
      {
          Extras = new List&lt;ProductChoiceExtra&gt;();
      }
  
      public int Quantity { get; set; }
      public int ProductId { get; set; }
      public List&lt;ProductChoiceExtra&gt; Extras { get; set; }
  }
  
  [Serializable]
  public class ProductChoiceExtra
  {
      public int ExtraProductId { get; set; }
      public string ExtraName { get; set; }
      public int ExtraQuantity { get; set; }
  }
  </code></pre>
  
  <p>And my user control code behind:</p>
  
  <pre><code>public partial class ProductDetails : System.Web.UI.UserControl
  {
      private Models.ProductChoice _productChoice;
  
      protected void Page_Load(object sender, EventArgs e)
      {
          _productChoice = new Models.ProductChoice()
          {
              Quantity = 1,
              ProductId = 1
          };
          _productChoice.Extras.Add(new Models.ProductChoiceExtra()
          {
              ExtraProductId = 101,
              ExtraName = "coke",
              ExtraQuantity = 1
          });
          _productChoice.Extras.Add(new Models.ProductChoiceExtra()
          {
              ExtraProductId = 104,
              ExtraName = "sprite",
              ExtraQuantity = 2
          });
  
      }
  
      public Models.ProductChoice GetProduct()
      {
          return _productChoice;
      }
  
      public void UpdateProduct(Models.ProductChoice model)
      {
          /* model.Extras is always null here, it should contain two ProductChoiceExtra objects */
  
          if (TryUpdateModel(_productChoice) == true)
          {
          }
      }
  }
  </code></pre>
  
  <p>My control markup:</p>
  
  <pre><code>&lt;div id="selectOptions"&gt;
      &lt;asp:FormView runat="server" ID="fvProductSelection" DefaultMode="Edit"
          ItemType="Models.ProductChoice"
          SelectMethod="GetProduct"
          UpdateMethod="UpdateProduct" &gt;
  
          &lt;EditItemTemplate&gt;
              &lt;asp:linkbutton id="UpdateButton" text="Update" commandname="Update" runat="server"/&gt;
              &lt;asp:HiddenField runat="server" ID="ProductId" Value="&lt;%# BindItem.ProductId %&gt;" /&gt;
              &lt;asp:TextBox Text ="&lt;%# BindItem.Quantity %&gt;" ID="Quantity" runat="server" /&gt;
  
              &lt;asp:Repeater ID="Extras" ItemType="Models.ProductChoiceExtra" DataSource="&lt;%# BindItem.Extras %&gt;" runat="server"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:HiddenField Value="&lt;%# BindItem.ExtraProductId %&gt;" ID="ExtraProductId" runat="server"  /&gt;
                      &lt;asp:Label Text="&lt;%# BindItem.ExtraName %&gt;" ID="Name" runat="server" /&gt;
                      &lt;asp:TextBox Text="&lt;%# BindItem.ExtraQuantity %&gt;" ID="Quantity"  runat="server" /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:Repeater&gt;
          &lt;/EditItemTemplate&gt;
      &lt;/asp:FormView&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I have tried making the <code>Extras</code> property a <code>BindingList</code> rather than a <code>List</code> but it didn't make any difference, the <code>Extras</code> collection isn't bound in the <code>UpdateProduct</code> method. </p>