title: How to dynamically generate controls in MVC?
body: <p>In asp.net, you can use asp:Repeater for dynamically generating controls.</p>
  
  <p>for example:</p>
  
  <pre><code>&lt;asp:Repeater runat="server" id="rpt"&gt;
     &lt;ItemTemplate&gt;
         &lt;asp:Textbox runat="server"/&gt;
     &lt;/ItemTemplate&gt;
  &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>Then you can get loop through the items on the server side:</p>
  
  <pre><code>foreach(var items in rpt.Items)
  {
  }
  </code></pre>
  
  <p>And in MVC, you can do something like this:</p>
  
  <pre><code>@foreach(var item in model)
  {
     @Html.TextBoxFor(a=&gt;item.searchCriteria);
  }
  </code></pre>
  
  <p>But there is a problem:</p>
  
  <p>I would like to generate textboxes when ever user clicks add more textbox button:</p>
  
  <p><a href="https://i.stack.imgur.com/Nn1NO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nn1NO.png" alt="enter image description here"></a></p>
  
  <p>If I use foreach in MVC, then I won't be able to fetch the user input on the controller, also, I don't have a model to loop through.</p>
  
  <p>And the final desired output will be:</p>
  
  <p>After the user clicks the "Add more textbox" button, a new textbox will be append to the bottom, and after the user clicks the search button, the text inside the textbox will be generated into a query string, something like this:</p>
  
  <p>View:</p>
  
  <pre><code> &lt;input name="searchCriteria" type="text" class="form-control" /&gt;
   &lt;input name="searchCriteria" type="text" class="form-control" /&gt;
   &lt;input name="searchCriteria" type="text" class="form-control" /&gt;
   &lt;input name="searchCriteria" type="text" class="form-control" /&gt;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code> public ActionResult Search()
          {
              string queryString = Request.RawUrl;
              //parse the querystring here
              return View(NumberOfVisitorsReport);
          }
  </code></pre>
  
  <p>And the query string generated will be:</p>
  
  <pre><code>www.website.com/?searchCriteria=a&amp;searchCriteria=b&amp;searchCriteria=c
  </code></pre>
  
  <p>So are there any alternative way to achieve this in MVC?</p>
  
  <p>Edit:</p>
  
  <p>I've took a look at some post on "How to generate controls dynamically in MVC"</p>
  
  <p>For example</p>
  
  <p><a href="https://stackoverflow.com/questions/26624270/control-creation-based-on-model-value-in-mvc4">Control creation based on model value in MVC4</a></p>
  
  <p>He is generating controls based on value in database, if I implement this to my website, the controls will lose value on each post back.</p>