使用数组将多个参数(无线电)从视图传递到控制器 [英] Passing multi parameters (radio) from view to controller using array
问题描述
我想在不使用Model的情况下,将55到100个可能在视图中为假或真的项目发布到我的控制器.这是我在Razor View中的代码:
I would like to post between 55 and 100 items which could be false or true from view to my controller without using Model. This is my code in Razor View:
@using(Html.BeginForm("User","User",FormMethod.Post,new{enctype="multipart/form-data"}))
{
<input type="radio" name="answer1">Choice one</input>
<input type="radio" name="answer1">Choice two</input>
<input type="radio" name="answer2">Choice three</input>
<input type="radio" name="answer2">Choice four</input>
....
<input type="radio" name="answer55">Choice fifty five</input>
<input type="radio" name="answer55">Choice fifty six</input>
<button type="submit">Send</button>
}
控制器:
[HttpPost]
public async Task<ActionResult> User(Array....)
{
return view();}
如何使用数组将所有参数(单选按钮)发送到控制器.感谢您的所有解决方案.
How can I send all of parameters (radio buttons) using an array to my controller. I appreciate all your solutions.
推荐答案
在视图中:要使数组起作用,请按以下格式命名单选按钮,以使模型绑定起作用
In View :For Arrays to work, name the Radio buttons in format as below for model binding to work
@using (Html.BeginForm("Index", "Home")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<input type="radio" name="param[0]" value="one" >
<input type="radio" name="param[0]" value="two" >
<input type="radio" name="param[1]" value="three" >
<input type="radio" name="param[1]" value="four" >
<input type="radio" name="param[2]" value="five" >
<input type="radio" name="param[2]" value="six" >
<input type="submit" value="submit" />
}
在Post ActionMethod中:
In Post ActionMethod :
[HttpPost]
public ActionResult Index(string[] param)
{ //
}
然后您将在action方法中获取param
数组中的值.
您可以根据需要设置任意数量的单选按钮, ModelBinder将负责绑定.
then you will get the value in param
array in the action method.
You can have as many number of Radio buttons as you wish & ModelBinder will take care of the binding.
这篇关于使用数组将多个参数(无线电)从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!