如何在 ASP.NET MVC 中枚举表单集合?

发布于 2024-07-17 11:34:12 字数 150 浏览 7 评论 0原文

我如何枚举所有的键/值 ASP.NET MVC 中的 FormCollection (system.web.mvc)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南…巷孤猫 2024-07-24 11:34:12

以下是专门针对 FormCollection 对象执行此操作的 3 种方法。

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

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];
  }
}
尾戒 2024-07-24 11:34:12
foreach(KeyValuePair<string, ValueProviderResult> kvp in form.ToValueProvider())
{
    string htmlControlName = kvp.Key;
    string htmlControlValue = kvp.Value.AttemptedValue;
}
foreach(KeyValuePair<string, ValueProviderResult> kvp in form.ToValueProvider())
{
    string htmlControlName = kvp.Key;
    string htmlControlValue = kvp.Value.AttemptedValue;
}
满身野味 2024-07-24 11:34:12
foreach(var key in Request.Form.AllKeys)
{
   var value = Request.Form[key];
}
foreach(var key in Request.Form.AllKeys)
{
   var value = Request.Form[key];
}
世态炎凉 2024-07-24 11:34:12

在.NET Framework 4.0中,使用ValueProvider的代码是:

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

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;
        }
回心转意 2024-07-24 11:34:12

我用这个:

string keyname;
string keyvalue;

for (int i = 0; i <= fc.Count - 1; i++)
{
    keyname = fc.AllKeys[i];
    keyvalue = fc[i];
}

希望它能帮助别人。

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.

走走停停 2024-07-24 11:34:12

在 VB.Net 中:

Dim fv As KeyValuePair(Of String, ValueProviderResult)
For Each fv In formValues.ToValueProvider
    Response.Write(fv.Key + ": " + fv.Value.AttemptedValue)
Next

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文