ASP.NET MVC中如何枚举formcollection?
我如何枚举ASP.NET MVC中 FormCollection(system.web.mvc)的所有键/值?
这里有3种方法可以用FormCollection对象来实现。
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]; } }
foreach(KeyValuePair<string, ValueProviderResult> kvp in form.ToValueProvider()) { string htmlControlName = kvp.Key; string htmlControlValue = kvp.Value.AttemptedValue; }
foreach(var key in Request.Form.AllKeys) { var value = Request.Form[key]; }
在.NET Framework 4.0中,使用ValueProvider的代码是:
IValueProvider valueProvider = formValues.ToValueProvider(); foreach (string key in formValues.Keys) { ValueProviderResult result = valueProvider.GetValue(key); string value = result.AttemptedValue; }
我使用这个:
string keyname; string keyvalue; for (int i = 0; i <= fc.Count - 1; i++) { keyname = fc.AllKeys[i]; keyvalue = fc[i]; }
希望它可以帮助别人。
在VB.Net中:
Dim fv As KeyValuePair(Of String, ValueProviderResult) For Each fv In formValues.ToValueProvider Response.Write(fv.Key + ": " + fv.Value.AttemptedValue) Next