title: Using FormCollection to store values from form
body: <p>I have a HTML Form page that sends data to my ActionResult </p>
  
  <p>The form has 1 to x amount of fields based on the size of the user shopping cart. </p>
  
  <p></p>
  
  <pre><code>&lt;form action="@Url.Action("AddToShoppingCart","ExpertEstimatorController")" method="post" name="shoppingCart"&gt;
      &lt;input type="hidden" name="Username" value="sampleUser" /&gt;
      &lt;input type="hidden" name="Password" value="BadPassword" /&gt;
  
      &lt;input type="hidden" name="item1" value="Apple"&gt;
      &lt;input type="hidden" name="quantity1" value="30"&gt;
  
      &lt;input type="hidden" name="item2" value="Orange"&gt;
      &lt;input type="hidden" name="quantity2" value="12"&gt;
  
      &lt;input type="hidden" name="item3" value="Peach"&gt;
      &lt;input type="hidden" name="quantity3" value="19"&gt;
  
      //size varies on shopping cart can be 1 item to x amount of items
  &lt;/form&gt;
  </code></pre>
  
  <p></p>
  
  <p>I want to be able to loop through all fields and store them in their own variable </p>
  
  <p>I been hard coding it and it is very inefficient </p>
  
  <pre><code>    public ActionResult AddToShoppingCart(System.Web.Mvc.FormCollection col)
      {
          string username = col["Username"];
          string password = col["Password"];
  
          string item1= col["item1"];
          string Qty1 = col["quantity1"];
  
          string item2 = col["item2"];
          string Qty2 = col["quantity2"];
  
          string item3= col["item3"];
          string Qty3 = col["quantity3"];
  
          string item4= col["item4"];
          string Qty4 = col["quantity4"];
  
          // 1 to x amount items 
  
          return ShoppingCartPage();
      }
  </code></pre>
  
  <p>I tried using</p>
  
  <pre><code>        List&lt;string&gt; list = new List&lt;string&gt;();
          foreach (var key in col.AsQueryable())
          {
              list.Add(col.Get(key.ToString()));
          }
  </code></pre>
  
  <p>to store each value into a list but I can't seem to get it to work </p>
  
  <p>Essentially I am just trying to run through each value and store it for later </p>