C# 多个单选按钮列表,获取控制器中的选定值

C# 多个单选按钮列表,获取控制器中的选定值,c#,asp.net-mvc,asp.net-mvc-4,razor,radiobuttonlist,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,Radiobuttonlist,我正在开发一个动态多选表单生成器。在这种情况下,表单可以有多个单选按钮列表,其中包含不同的问题和选项 问题1 选择1 选择2 选择3 选择4 问题2 选择5 选项6在html文件中,您需要使用指定名称对无线电输入进行分组 首先,你必须改变你的问答模式来建立关系 public class ClsQuestions { // Parent ID to be referenced by children public int ID { get;

我正在开发一个动态多选表单生成器。在这种情况下,表单可以有多个单选按钮列表,其中包含不同的问题和选项

问题1

选择1 选择2 选择3 选择4
问题2
选择5
选项6
在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 />
}
//所有问题的循环
@foreach(有问题的var项目){
//获取与父问题相关的所有选项的列表
var options=lstOptions.Where(x=>x.QuestionID==item.ID).ToList();
@(项目问题)

//所有找到的当前问题选项上的Al循环 @foreach(期权中的var期权){ //使用name属性对选项进行分组('Question'+QuestionID) @(可选。可选) }
}
现在,当您提交表单时,可以发送所选选项的数组。

是否要Post方法中单选按钮的值?是选定值已调试并检查
SelectedAnswer
数组中的内容?发帖后?请在问题中提供代码,而不是容易再现问题或看到实际工作的图像。您的模型是错误的-您需要一个(比如)带有属性的
类问题vm
,列出可能的答案
,以及
[必需]字符串选择答案
,然后将该视图模型的列表传递给视图