简体   繁体   English

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

[英]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.

相关问题 获取选定的单选按钮不在ASP .NET的列表中 - Get selected radio button not in a list in ASP .NET 如何从单选按钮列表和复选框列表中获取选定的值? - How can I get selected values from radio button list and check box list? 如何使用jQuery获取或设置单选按钮列表的选定索引? - How to get or set Selected index of the Radio Button List using jquery? 将单选按钮值传递给 controller - Passing radio button values to the controller 如何选中单选按钮以及如何仅控制在列表视图中选择的一个单选按钮 - How to get checked radio button and how to control only one radio button selected in list view 无法检查控制器中单选按钮的选择值 - not able to check the radio button selected value in controller 如何获取嵌套在数据列表中的单选按钮列表的文本值 - How to Get Text Values of a Radio button list that is nested in a data list 如何从WPF列表框中获取选定列表项中的选定单选按钮 - How to get selected radio button in selected list item from wpf listbox 从MVC3控制器中的多个选择列表中获取选定值 - Get selected values from multiple selectlists in MVC3 controller 单选按钮列表所选项目值发行 - Radio button list selected item value issu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM