Form Collection 3 approaches

Here are 3 ways to get your values with a FormCollection object.


public ActionResult SomeActionMethod(FormCollection formCollection)
{

//Cycling through the AllKeys

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

//By Each Key

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

 
 // Using the ValueProvider

     IValueProvider valueProvider = formCollection.ToValueProvider();
  
    foreach (string key in formCollection.Keys)
    {
      ValueProviderResult result = valueProvider.GetValue(key);
      string value = result.AttemptedValue;
    }
}

Comments

Popular posts from this blog

Linq Exclude from separate list

Sorting Ascending and Descending

Linq Query Syntax vs Method Syntax