title: Pass dynamic model to controller
body: <p>I have table which is filled with some data, when I click "addTemplate" in my controller I don't get any data from table. </p>
  
  <p>Here is my index.cshtml</p>
  
  <pre><code>@model ReportGenerator.WebUI.Models.FieldViewModel
  @{
      var list = @Model.IEnumerableField.GroupBy(item =&gt; item.TemplateName).ToList();
  }
  
  @using (Html.BeginForm("addTemplate", "TemplateField", FormMethod.Post))
          {
              &lt;div class="form-group"&gt;
                  &lt;div class="input-group"&gt;
                      &lt;label class="label label-default"&gt;Nazwa szablonu*&lt;/label&gt;
                      @Html.TextBoxFor(m =&gt; m.field.TemplateName, new { @class = "form-control" })
                      &lt;br /&gt;&lt;br /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;table id="sort" class="table"&gt;
                  &lt;thead&gt;
                      &lt;tr&gt;
                          &lt;th&gt;Nazwa kolumny z BD*&lt;/th&gt;
                          &lt;th&gt;Długość rekordu*&lt;/th&gt;
                          &lt;th&gt;Nazwa kolumny wyświetlana&lt;/th&gt;
                          &lt;th&gt;Format*&lt;/th&gt;
                      &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tbody id="tblsort"&gt;
                      @for (var i = 0; i &lt; Model.listOfFormats(Model.field.TemplateName).ColumnNamesDB.Count; i++)
                      {
                          &lt;tr&gt;
                              &lt;td&gt;@Html.TextBoxFor(m =&gt; Model.listOfFormats(Model.field.TemplateName).ColumnNamesDB[i], new { @class = "form-control" })&lt;/td&gt;
                              &lt;td&gt;@Html.TextBoxFor(m =&gt; Model.listOfFormats(Model.field.TemplateName).LengthColumns[i], new { @class = "form-control" })&lt;/td&gt;
                              &lt;td&gt;@Html.TextBoxFor(m =&gt; Model.listOfFormats(Model.field.TemplateName).ColumnNamesUser[i], new { @class = "form-control" })&lt;/td&gt;
                              &lt;td&gt;@Html.DropDownListFor(m =&gt; Model.listOfFormats(Model.field.TemplateName).Formats[i], new SelectList(Model.formatFieldValues, "Value", "Text"), Model.listOfFormats(Model.field.TemplateName).Formats[i], new { @class = "form-control" })&lt;/td&gt;
                          &lt;/tr&gt;
                      }
                  &lt;/tbody&gt;
              &lt;/table&gt;
  
              &lt;div id="hiddenHtml" style="display:none;"&gt;
                  @Html.DropDownList("FormatFieldName", new SelectList(Model.formatFieldValues, "Value", "Text"), "Domyślny", new { @class = "form-control" })
              &lt;/div&gt;
              &lt;button class="btn btn-success" type="button" id="addRow" style="float: right;"&gt;&lt;span class="glyphicon glyphicon-plus" id="addRow"&gt;&lt;/span&gt;&lt;/button&gt;
              &lt;button class="btn btn-success" id="save" style="float: left;"&gt;Zapisz&lt;/button&gt;
              &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
              &lt;div&gt;
                  &lt;p style="float: right;"&gt;* - Pole oznaczone gwiązdką są obowiązkowe!&lt;/p&gt;
              &lt;/div&gt;
              &lt;br /&gt; &lt;br /&gt;
              @Html.ValidationSummary()
              &lt;h1 style="color:@ViewBag.color"&gt;@ViewBag.Info&lt;/h1&gt;
          }
  </code></pre>
  
  <p>Here is my model</p>
  
  <pre><code>public class FieldViewModel
  {
      public TemplateField field { get; set; }
      public IEnumerable&lt;TemplateField&gt; IEnumerableField;
      public Fields fieldObj {get;set;}
  
  
      public Fields listOfFormats(string nameTemplate)
      {
          Fields obj = new Fields();
          var items = IEnumerableField.Where(i =&gt; i.TemplateName == nameTemplate).ToList();
          if (items != null)
          {
              foreach (var x in items)
              {
                  obj.ColumnNamesDB.Add(x.ColumnNameDB);
                  obj.Formats.Add(x.Format);
                  obj.LengthColumns.Add(x.Length.ToString());
                  obj.ColumnNamesUser.Add(x.ColumnNameUser);
              }
          }
          return obj;
      }
  }
  </code></pre>
  
  <p>and finally my controller method</p>
  
  <pre><code>public ActionResult addTemplate(FieldViewModel model)
  {
      return ? // model is empty, there is nothing...
  }
  </code></pre>
  
  <p>I think that I made some mistake in Model, can someone look at this? 
  Thanks for any help!</p>
  
  <p>EDIT:</p>
  
  <p>Here is Fields class:</p>
  
  <pre><code>public class Fields
  {
      public List&lt;string&gt; ColumnNamesDB = new List&lt;string&gt;();
      public List&lt;string&gt; LengthColumns = new List&lt;string&gt;();
      public List&lt;string&gt; Formats = new List&lt;string&gt;();
      public List&lt;string&gt; ColumnNamesUser = new List&lt;string&gt;();
  }
  </code></pre>
  
  <p>and TemplateField cass</p>
  
  <pre><code> public class TemplateField
  {
      [Key]
      public int FieldId { get; set; }
  
      [Required(ErrorMessage = "Proszę podać nazwę kolumny z bazy danych.")]
      public string ColumnNameDB { get; set; }
      [Required(ErrorMessage = "Proszę podać długość rekordu.")]
      public int Length { get; set; }
  
      public string Format { get; set; }
      [Required(ErrorMessage = "Proszę podać nazwę szablonu.")]
      public string TemplateName { get; set; }
      [Required(ErrorMessage = "Proszę podać nazwę kolumny jaka będzie wyświetlana.")]
      public string ColumnNameUser { get; set; }
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Ok I have made a lot of miastakes here so this question don't have an answer I think.</p>