多个单选按钮列表,在控制器中获取选定的值 [英] Multiple radio button list, get selected values in controller
本文介绍了多个单选按钮列表,在控制器中获取选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个动态多选表单构建器。在这种情况下,表单可以有多个单选按钮列表,其中包含不同的问题和选项。
我想在控制器中获取所选选项。
我尝试了以下代码。它不起作用。
我尝试过:
型号
I am working on a dynamic multiple choice form builder. In this scenario, the form can have multiple radio button list with a different question and options.
I want to get the selected options in the controller.
I tried the following code. It's not working.
What I have tried:
Model
public class clsMain
{
public string[] selectedAnswer { get; set; }
public List<ClsQuestions> lstQuestion { get; set; }
public List<ClsOptions> lstOptions { get; set; }
}
public class ClsQuestions
{
public string question { get; set; }
}
public class ClsOptions
{
public int optionid { get; set; }
public string optionvalue { get; set; }
public string optionlable { get; set; }
}
控制器
controller
[HttpPost]
public ActionResult FromSelectedValues(clsMain model)
{
return View();
}
查看
View
@for (int i = 0; i < 2; i++){
Question @i
<br />
@Html.RadioButtonFor(m => m.SelectedAnswer[i], "Answer1"+i)
<label>Answer1 @i</label>
@Html.RadioButtonFor(m => m.SelectedAnswer[i], "Answer2"+i)
<label>Answer2 @i</label>
@Html.RadioButtonFor(m => m.SelectedAnswer[i], "Answer3"+i)
<label>Answer3 @i</label>
@Html.RadioButtonFor(m => m.SelectedAnswer[i], "Answer4"+i)
<label> Answer4 @i</label>
}
推荐答案
您可以尝试这样的事情:
You could try doing something like this:
[HttpPost]
public ActionResult FromSelectedValues(clsMain model)
{
if (ModelState.IsValid)
{
for (int i =0; i < model.lstQuestion.Count; i++)
{
var selectedAnswer = model.SelectedAnswer[i];
// do something here
}
}
return View();
}
}
PS:我从未测试过,但它应该可以帮助你开始如何通过它。另外,请查看本文以获取有关使用MVC中的单选按钮的参考: ASP.NET MVC中的单选按钮列表 [ ^ ]
这篇关于多个单选按钮列表,在控制器中获取选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文