title: MVC dynamic form controls binding to model
body: <p>I'm trying to dynamically create a form with different types of fields. Then simply pass the user inputted data back to the controller and bind back to my model.  I'm using a custom editor template for each control and was hoping it would bind properly in the controller. However, the property is NULL each time so I cannot retrieve the input values.</p>
  
  <p>Model</p>
  
  <pre><code>public class ReceiptModel : ClassBase
  {
      public int ReceiptId { get; set; }
      public List&lt;CustomControlModel&gt; CustomControlList { get; set; }
  }
  
  public class CustomControlModel 
  {
      public string CustomControlName { get; set; }
      public CustomControlType CustomControlType { get; set; }
  }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>@foreach (CustomControlModel ccm in @Model.CustomControlList)
  {
      if (!string.IsNullOrEmpty(ccm.PropertyName))
      {
          @Html.EditorFor(model =&gt; ccm, "CustomControlModel")
      }
  }
  </code></pre>
  
  <p>Custom Template</p>
  
  <pre><code>@Html.HiddenFor(model =&gt; model.CustomControlId)
  
  
  &lt;label&gt;@Model.LabelCaption&lt;/label&gt;
  
  @switch (@Model.CustomControlType)
  {
  
  case CustomControlType.TEXTBOX:
                      if (@Model.ReadOnly)
                      {
  
                          @Html.TextBoxFor(model =&gt; model.CustomControlId, new { @readonly = "readonly", @Value = @Model.Value })
                      }
                      else
                      {
  
          &lt;input id="@Model.CustomControlName" name="@Model.CustomControlName" type="text" value="@Model.Value" /&gt;
  }
  </code></pre>
  
  <p>Any help would be much appreciated. Thanks in advance.</p>