title: Add dynamic form fields in ASP.NET Core MVC App
body: <p>In my app I want to make model that can have dynamic list of attributes:</p>
  
  <blockquote>
    <p>Provider class</p>
  </blockquote>
  
  <pre><code>using System;
  using System.Collections.Generic;
  
  namespace DynamicForms.Models
  {
      public class Provider
      {
          public String Name { get; set; }
          public List&lt;Attribute&gt; Attributes { get; set; }
  
          public Provider()
          {
              Attributes = new List&lt;Attribute&gt;();
          }
      }
  }
  </code></pre>
  
  <p>My controller has two methods. One to add more attributes to current model and one to save Provider model. Method to add more attributes looks like this:</p>
  
  <pre><code>    [HttpPost]
      // Add attribute to current provider model
      public IActionResult Index(Provider provider)
      {
          provider.Attributes.Add(new Models.Attribute());
          return View(provider);
      }
  </code></pre>
  
  <p>And my view looks like this:</p>
  
  <pre><code>@model Provider
  @using (Html.BeginForm())
  {
      &lt;div&gt;
          @Html.TextBoxFor(m =&gt; m.Name)
          &lt;input type="submit" value="Add attribute" formmethod="post"/&gt;
      &lt;/div&gt;
      &lt;div&gt;
          @foreach ( var attribute in Model.Attributes)
          {
          &lt;div&gt;
              @Html.TextBoxFor(a =&gt; attribute.Name)
              @Html.TextBoxFor(a =&gt; attribute.Value)
          &lt;/div&gt;
          }
      &lt;/div&gt;
      &lt;div&gt;
          &lt;input type="submit" value="Save form" formaction="Provider/Save" formmethod="post"/&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>When I press "Add attribute" button attribute is added and a row of input fields is appearing. However when i press the button one more time nothing happens. No another row is added. Model attributes count is still 1. I've been looking for solutions all over the web but couldn't find anything that fixes my problem. How can I make form fields be added dynamically to model and displayed in vieW?</p>