ASP.NET MVC C#动态单选按钮获得的FormCollection [英] ASP.NET MVC C# dynamic radiobuttons getting formcollection

查看:270
本文介绍了ASP.NET MVC C#动态单选按钮获得的FormCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这一点就不可能在视图中动态生成多个单选按钮,提交表单上我如何在控制器中收集的值的形式。比如有像性别(男,女),动态生成的单选按钮,教育(学士,硕士,BCS)。我怎么能有有在容器中的价值。

i have a form in which there can be multiple radio buttons generated dynamically in the view ,on the form submitted how can i collect the values in the controller. For example there are radio buttons dynamically generated like Gender (Male and Female), Education (BS,MS,BCS) . how can i have there value in the container.

推荐答案

如果你知道你可以使用动作参数单选按钮的名称。如果名称是任意的,你可以从的FormCollection读取值传递给你的POST操作参数(这是一个的NameValueCollection 所以,你可以通过按键循环,并获得相应的值)。

If you know the names of the radio buttons you could use action parameters. If the names are arbitrary you could fetch the values from a FormCollection parameter passed to your POST action (it is a NameValueCollection so you could loop through the key and get the corresponding values).

我个人使用确定性的名字建议你:

Personally I would recommend you using deterministic names:

Gender:
<input type="radio" value="M" name="radios[0]" />
<input type="radio" value="F" name="radios[0]" />

Education:
<input type="radio" value="BS" name="radios[1]" />
<input type="radio" value="MS" name="radios[1]" />
<input type="radio" value="BCS" name="radios[1]" />

而在你的控制器动作,你可以使用一个集合:

And in your controller action you could use a collection:

[HttpPost]
public ActionResult Update(string[] radios)
{
    // The radios collection will contain the selected values like:
    // radios[0] = "F"
    // radios[1] = "MS"
    ...
}

这篇关于ASP.NET MVC C#动态单选按钮获得的FormCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆