title: Dynamically creating hidden form fields for various models (MVC 4)
body: <p>I'm trying to dynamically create hidden fields for a set of properties, but I'm getting a 500 server error when I submit the form. I confirmed the following:</p>
  
  <ul>
  <li>The properties I'm iterating over in the foreach statement are correct.</li>
  <li>property.Name is a valid property name for the type retrieved by NewItem.GetType()</li>
  </ul>
  
  <p>Here's what I have:</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model PaneViewModel
  
  using (Ajax.BeginForm("AddItem", "Action", new AjaxOptions
  {
      UpdateTargetId = "tool-wrapper",
      HttpMethod = "POST",
  }))
  {
      // Some standard input fields here (these are working properly).
  
      [...]
  
      // Here's what's broken:
      @foreach (var property in Model.NewItem.GetType().GetProperties().Where(&lt;criteria here&gt;))
      {
          @Html.HiddenFor(m =&gt; m.NewItem.GetType().GetProperty(property.Name), column.GetValue(Model.NewItem, null))
      }
  
      &lt;button type="submit"&gt;Add&lt;/button&gt;
  }
  </code></pre>
  
  <p><strong>ItemViewModel</strong></p>
  
  <pre><code>public class ItemViewModel
  {
      public int SomeField { get; set; }
      public int AnotherField { get; set; }
  }
  </code></pre>
  
  <p><strong>PaneViewModel</strong></p>
  
  <pre><code>public class PaneViewModel
  {
      public ItemViewModel NewItem { get; set; }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>[HttpPost]
  public ActionResult AddItem([Bind(Prefix = "NewItem")] ItemViewModel model)
  {
      // Stuff here.
  }
  </code></pre>
  
  <p>It's worth noting that the following generates the hidden fields with the correct names and values in the generated HTML, but the values of the hidden field aren't posted to the controller action:</p>
  
  <pre><code>@foreach (var property in Model.NewItem.GetType().GetProperties().Where(&lt;criteria here&gt;))
  {
      @Html.Hidden(property.Name, column.GetValue(Model.NewItem, null))
  }
  </code></pre>
  
  <p>So it seems the problem is with the <code>m =&gt; m.NewItem.GetType().GetProperty(property.Name)</code> component</p>