我正在研究动态的多项选择表单生成器。在这种情况下,表单可以具有多个带有不同问题和选项的单选按钮列表。
Value
我为此建模:
<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>
控制器
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; }
}
查看
[HttpPost]
public ActionResult FromSelectedValues(clsMain model)
{
return View();
}
简而言之,我想在控制器中获得选定的选项。
答案 0 :(得分:0)
在您的html文件中,您需要使用指定名称对单选输入进行分组。
首先,您必须更改问答模型以建立联系。
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; }
}
您将这样更改视图
// 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 />
}
现在,当您提交表单时,可以发送一系列选择的选项。 Here is a post for sending selected options to server