How can a formcollection be enumerated in ASP.NET MVC?

Posted by imomins on June 4, 2012 at 6:25 AM

 

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];

  }

}


Categories: ASP.NET, C#, Personal

Post a Comment

Oops!

Oops, you forgot something.

Oops!

The words you entered did not match the given text. Please try again.

You must be a member to comment on this page. Sign In or Register

1 Comment

Reply imomins
6:53 AM on June 4, 2012 
How can a formcollection be enumerated in ASP.NET MVC?