43

How can I enumerate through all the key/values of a FormCollection (system.web.mvc) in ASP.NET MVC?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mrblah
  • 99,669
  • 140
  • 310
  • 420
  • Further how can we find if the item was returned from a text box or a hidden field or a combo box etc? – Thunder Feb 19 '13 at 09:49

6 Answers6

91

Here are 3 ways to do it specifically with a FormCollection object.

public ActionResult SomeActionMethod(FormCollection formCollection)
{
  foreach (var key in formCollection.AllKeys)
  {
    var value = formCollection[key];
  }

  foreach (var key in formCollection.Keys)
  {
    var value = formCollection[key.ToString()];
  }

  // Using the ValueProvider
  var valueProvider = formCollection.ToValueProvider();
  foreach (var key in valueProvider.Keys)
  {
    var value = valueProvider[key];
  }
}
Steve Willcock
  • 26,111
  • 4
  • 43
  • 40
  • Please note: ToValueProvider() changed between framework 3.5 and 4.0 – Colin Apr 19 '13 at 16:12
  • can i create or [Add](https://msdn.microsoft.com/en-us/library/system.web.mvc.formcollection.add(v=vs.118).aspx) values to `formcollection` from model [properties](http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class) in key value pair ? – Shaiju T Jan 17 '17 at 15:34
6
foreach(KeyValuePair<string, ValueProviderResult> kvp in form.ToValueProvider())
{
    string htmlControlName = kvp.Key;
    string htmlControlValue = kvp.Value.AttemptedValue;
}
  • 2
    This applies to Framework 3.5 where ToValueProvider returns an IDictionary. In Framework 4.0 ToValueProvider returns an IValueProvider. – Colin Apr 19 '13 at 16:11
4
foreach(var key in Request.Form.AllKeys)
{
   var value = Request.Form[key];
}
James Avery
  • 3,062
  • 1
  • 20
  • 26
1

I use this:

string keyname;
string keyvalue;

for (int i = 0; i <= fc.Count - 1; i++)
{
    keyname = fc.AllKeys[i];
    keyvalue = fc[i];
}

hope it helps someone.

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Losbear
  • 3,255
  • 1
  • 32
  • 28
1

In .NET Framework 4.0, the code to use the ValueProvider is:

        IValueProvider valueProvider = formValues.ToValueProvider();
        foreach (string key in formValues.Keys)
        {
            ValueProviderResult result = valueProvider.GetValue(key);
            string value = result.AttemptedValue;
        }
Colin
  • 22,328
  • 17
  • 103
  • 197
0

And in VB.Net:

Dim fv As KeyValuePair(Of String, ValueProviderResult)
For Each fv In formValues.ToValueProvider
    Response.Write(fv.Key + ": " + fv.Value.AttemptedValue)
Next
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Dave
  • 917
  • 1
  • 8
  • 20
  • I get "Expression is of type 'System.Web.Mvc.IValueProvider', which is not a collection type" when I try this. If I leave out the "ToValueProvider" it compiles, but I get "Specified cast is not valid." – DrydenMaker Dec 27 '09 at 06:02
  • @DrydenMaker - that is because you are using 4.0. This answer applies to 3.5 – Colin Apr 19 '13 at 16:26