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

后端 未结 6 1883
南旧
南旧 2020-11-29 20:28

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

相关标签:
6条回答
  • 2020-11-29 20:54
    foreach(var key in Request.Form.AllKeys)
    {
       var value = Request.Form[key];
    }
    
    0 讨论(0)
  • 2020-11-29 20:55

    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
    
    0 讨论(0)
  • 2020-11-29 21:03

    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.

    0 讨论(0)
  • 2020-11-29 21:05

    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];
      }
    }
    
    0 讨论(0)
  • 2020-11-29 21:07
    foreach(KeyValuePair<string, ValueProviderResult> kvp in form.ToValueProvider())
    {
        string htmlControlName = kvp.Key;
        string htmlControlValue = kvp.Value.AttemptedValue;
    }
    
    0 讨论(0)
  • 2020-11-29 21:09

    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;
            }
    
    0 讨论(0)
提交回复
热议问题