How can I enumerate through all the key/values of a FormCollection (system.web.mvc) in ASP.NET MVC?
A:
foreach(var key in Request.Form.AllKeys)
{
var value = Request.Form[key];
}
James Avery
2009-04-18 12:46:20
+24
A:
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
2009-04-18 18:58:39
A:
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
Dave
2009-08-13 16:55:26
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
2009-12-27 06:02:29