title: How to Bind a List in Model with HTML Form in ASP.NET Core?
body: <p>I'm unsure what I'm doing wrong when trying to map form values to my model, <code>FieldMappingCollection</code>. I've been able to get the Id and Name back for the model but not the list of mappings, either the previously existing or newly created ones.</p>
  <p>The page contains a text box for the mapping name and a table of rows which contains a select list and a text box. The FromField relates to the select list and the ToField relates to the text box.</p>
  <p>I'm not super familiar with ASP.NET Core and even less familiar with older versions, so I'm unsure if <code>@Html...</code> is totally phased out or what the proper syntax is for automating binding or how to implement custom binding or HTML generators etc. I'm also not concerned with exactly how I should be handling the select list in particular, it's a bit of temporary code to get the page working and I'll come back to it later.</p>
  <pre class="lang-cs prettyprint-override"><code>public class FieldMappingCollection
  {
      public int Id { get; set; }
      public string Name { get; set; } = &quot;&quot;;
  
      public List&lt;FieldMapping&gt; FieldMappings { get; set; } = new();
  }
  
  public class FieldMapping
  {
      public int Id { get; set; }
  
      public string FromField { get; set; } = &quot;&quot;;
      public string ToField { get; set; } = &quot;&quot;;
  }
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>public class MyController
  {
      public static List&lt;string&gt; AvailableFields = new()
      {
          // predefined field names for select list...
      };
  
      private readonly IMappingRepository m_repo;
  
      // constructor, Index, etc...
  
      public IActionResult EditMappingCollection(int id)
      {
          return View(m_repo.GetById(id));
      }
  
      [HttpPost]
      public IActionResult EditMappingCollection(FieldMappingCollection model)
      {
          m_repo.Update(model);
          m_repo.Save();
  
          return RedirectToAction(nameof(Index));
      }
  }
  </code></pre>
  <pre class="lang-html prettyprint-override"><code>@model FieldMappingCollection
  
  // other required stuff...
  
  &lt;form asp-action=&quot;EditMappingCollection&quot; method=&quot;post&quot;&gt;
      &lt;input hidden asp-for=&quot;Id&quot; type=&quot;number&quot; /&gt;
      &lt;input type=&quot;text&quot; asp-for=&quot;Name&quot; /&gt;
      &lt;table class=&quot;table&quot;&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th scope=&quot;col&quot;&gt;From Field&lt;/th&gt;
                  &lt;th scope=&quot;col&quot;&gt;To Field&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
              @for (int i = 0; i &lt; Model.FieldMappings.Count; i++)
              {
                  &lt;tr&gt;
                      // Nothing to store the mapping Id yet
                      &lt;td&gt;&lt;select asp-for=&quot;FieldMappings[@i].FromField&quot; style=&quot;width: 100%;&quot; asp-items=&quot;@MyController.AvailableFields.Select(field =&gt; new SelectListItem(field, field, Model.FieldMappings[i].FromField == field))&quot;&gt;&lt;/select&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input asp-for=&quot;FieldMappings[@i].ToField&quot; style=&quot;width: 100%;&quot; type=&quot;text&quot; placeholder=&quot;To Field&quot; value=&quot;@Model.FieldMappings[i].ToField&quot; /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              }
          &lt;/tbody&gt;
      &lt;/table&gt;
      &lt;input class=&quot;btn btn-primary&quot; type=&quot;submit&quot; value=&quot;Save Changes&quot; /&gt;
  &lt;/form&gt;
  
  </code></pre>