title: Populating a form dynamically based on user input in ASP.Net MVC
body: <p>My question is similar to Engram's <a href="https://stackoverflow.com/questions/223149/how-do-you-handle-the-output-of-a-dynamically-generated-form-in-aspnet-mvc">here</a>, but my question goes a bit further.  The way i intend it to work is I have a textbox asking how many entries a user is going to make.  After they input the number, I need to create that many more textboxes to allow for entries (and then repeat the same process with those textboxes, but baby steps first...)  I tried collecting the keys on the post, but it only returns the initial textbox asking for the number of entries.  I'm still trying to get a grasp on MVC and the tutorials/videos so far don't delve this deep into it yet.  Then again, I know this is probably something I could handle using JQuery, but I'd still be stuck in the same situation.</p>
  <p>This is the controller I'm using:</p>
  <pre><code>[AcceptVerbsAttribute(&quot;POST&quot;)]
      public ActionResult Create(int tbxNumberOfExercises)
      {
          ViewData[&quot;number&quot;] = tbxNumberOfExercises;
          foreach (var key in Request.Form.Keys)
          {
              string keyString = key.ToString();
              if (keyString.StartsWith(&quot;tbox_exercise&quot;, StringComparison.OrdinalIgnoreCase))
              {
                  string recNum = keyString.Substring(13, keyString.Length - 13);
                  string approvedKey = Request.Form[&quot;tbox_exercise&quot; + recNum];
                  int number;
                  int.TryParse(approvedKey, out number);
              }
          }
          return View(&quot;Create&quot;);
      }
  </code></pre>
  <p>And this is my aspx:</p>
  <pre><code>&lt;form action=&quot;/CreateWorkout/Create&quot; method=&quot;post&quot;&gt;
  Number of Exercises:
  &lt;%= Html.TextBox(&quot;tbxNumberOfExercises&quot;) %&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  &lt;input type=&quot;submit&quot; value=&quot;Set Exercise Number&quot; /&gt;
  &lt;/form&gt;
  &lt;% if (ViewData[&quot;number&quot;] != null)%&gt;
  There are this many:&lt;%=Html.Encode(ViewData[&quot;number&quot;])%&gt;
  &lt;br /&gt;
  and this line should show up
  &lt;% if (ViewData[&quot;number&quot;] != null)
     {
         int max = (int)ViewData[&quot;number&quot;];
  
         for (int i = 0; i &lt; max; i++)
         {%&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;%= Html.TextBox(&quot;tbox_exercise&quot; + i) %&gt;
      &lt;% }
     } %&gt;
  &lt;% if (ViewData[&quot;s&quot;] != null) %&gt;
  &lt;%=Html.Encode(ViewData[&quot;s&quot;]) %&gt;
  </code></pre>
  <p>Is there something I'm overlooking, not comprehending, or should I quit while I'm at it because it seems like I'll never get it?</p>
  <p>Thanks in advance for any help -- I'm just trying to learn as most I can.</p>