【问题标题】:Multiple radio button list, get selected values in controller多个单选按钮列表,在控制器中获取选定的值
【发布时间】:2018-08-19 17:45:40
【问题描述】:

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

<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();
    }

查看

@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>    
}

简而言之,我想在控制器中获得选定的选项。

【问题讨论】:

  • 你想要Post方法中单选按钮的值吗?
  • 是选择的值
  • 你调试过SelectedAnswer数组里面的内容了吗?发布后?
  • 并请在问题中提供代码而不是易于重现问题或查看实际工作的图像
  • 你的模型错了——你需要一个(比如)class QuestionVM 属性为List&lt;ClsOptions&gt; PossibleAnswers[Required]string Selected Answer,然后你将一个视图模型的列表传递给视图

标签: c# asp.net-mvc asp.net-mvc-4 razor radiobuttonlist


【解决方案1】:

在您的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 2016-08-30
    • 2015-09-25
    • 1970-01-01
    • 2016-07-17
    相关资源
    最近更新 更多