Back to list
Views:   105.3K
Replies:  1
Archived
Extract key/values from FormCollection in ASP.NET MVC action method
It took me a while to find this out.  To avoid you from having to chase for the same information: here's how you extract key / value pairs from the FormCollection that comes as an argument in an ASP.NET MVC action method:

public ActionResult Create(FormCollection formCollection)
{

   foreach (var key in formCollection.AllKeys)
   {
      var value = formCollection[key];
      // etc.
   }

   foreach (var key in formCollection.Keys)
   {
       var value = formCollection[key.ToString()];
       // etc.
   }
}
Also, a final tip: If you find that the formCollection does not contain the key/value pairs you expected, check that 
  1. Your controls reside within the correct form (you can have more than one)
  2. Your controls have names (just having an id is not returning values in the formCollection)
Hope this helps. 
Christopher Ronny, Apr 18, 2010
Reply 1
Nice and timely information. Exactly what I was looking for. +1 for you.
Juan Perez, Apr 19, 2010
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas