title: ASP MVC 3 RAZOR dynamic form generation post
body: <p>I'm trying to create asp mvc 3 page with a form with text fields that are dynamically generated based on the number of object in a list that is in the model I passed into page controller.  The problem I'm running into is that whenever I post to the controller the list maintains the values but not the names of the objects in the list.  Here is an example:</p>
  
  <p>The Model:</p>
  
  <pre><code>public class SomeModel
      {
          List&lt;Field&gt; fieldList;
          public SomeeModel()
          {
              fieldList = new List&lt;Field&gt;();
          }
  
  
          public string Name { get; set; }
  
          public List&lt;Field&gt; FieldList
          {
              get
              {
                  return fieldList;
              }
          }
  
      }
  </code></pre>
  
  <p>The controller:</p>
  
  <pre><code>public ActionResult Preview()
  {
      SomeModel model = new SomeModel();
      model.FieldList.Add( new Field { name = "test 1"});
      model.FieldList.Add( new Field { name = "test 2"});
      model.FieldList.Add( new Field { name = "test 3"});
  
      return View(model);
  }
  </code></pre>
  
  <p>The View:</p>
  
  <pre><code>@using (Html.BeginForm()) {
      &lt;div&gt;
          &lt;fieldset&gt;
              &lt;legend&gt;Input Field List&lt;/legend&gt;
  
              @for (var i = 0; i &lt; Model.FieldList.Count(); i++)
              {
                  &lt;div class="editor-label"&gt;
                     @Html.Label(Model.FieldList[i].name)
                 &lt;/div&gt;
                 &lt;div class="editor-field"&gt;
                     @Html.TextBoxFor(model =&gt; Model.FieldList[i].value)
                 &lt;/div&gt;
              }
  
               &lt;p&gt;
                  &lt;input type="submit" value="Generate PDF" /&gt;
              &lt;/p&gt;
          &lt;/fieldset&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>I saw a similar post here <a href="https://stackoverflow.com/questions/6428571/net-mvc-razor-dynamic-form-generation">.NET MVC Razor Dynamic Form Generation</a> but answer runs into the same issues as I got.  Hope I'm being clear!  If not I'll post more info.  Thanks!</p>