[英]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. 在这种情况下,该窗体可以具有带有不同问题和选项的多个单选按钮列表。
<strong>Question 1 </strong> <br/> <input type="radio" value=""><lable>Option1</lable> <input type="radio" value=""><lable>Option2</lable> <input type="radio" value=""><lable>Option3</lable> <input type="radio" value=""><lable>Option4</lable> <br /> <strong>Question 2 </strong> <br /> <input type="radio" value=""><lable>Option5</lable> <input type="radio" value=""><lable>Option6</lable>
I make model for this: 我为此建模:
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
@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>
}
In short, I want to get selected options in the controller. 简而言之,我想在控制器中获得选定的选项。
In your html file you need to group radio inputs with a specified name. 在html文件中,您需要使用指定名称对单选输入进行分组。
First of all, you have to change your question and answer model to make a relation. 首先,您必须更改您的问答模型以建立联系。
public class ClsQuestions
{
// Parent ID to be referenced by children
public int ID { get; set; }
public string question { get; set; }
}
public class ClsOptions
{
// Parent question id of the option
public int QuestionID { get; set; }
public int optionid { get; set; }
public string optionvalue { get; set; }
public string optionlable { get; set; }
}
You would change your view like this 你会这样改变你的看法
// A loop on all of the questions
@foreach(var item in lstQuestion) {
// Get list of all options that related to parent question
var options = lstOptions.Where(x => x.QuestionID == item.ID).ToList();
<strong>@(item.question)</strong>
<br/>
// Al loop on all found options of current question
@foreach(var option in options) {
// Group options by using the name property ('Question'+QuestionID)
<input type="radio" value="@(option.optionid)" name="Question@(item.ID)"><lable>@(option.optionlable)</lable>
}
<br />
}
Now, when you're submitting your form, you can send an array of selected options. 现在,当您提交表单时,可以发送一系列选择的选项。 Here is a post for sending selected options to server 这是将选定的选项发送到服务器的帖子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.