简体   繁体   English

@ Html.RadioButtonFor获得选择的选项,允许单选

[英]@Html.RadioButtonFor get selected option with single selection allowed

I would like to have two groups of Radio buttons in a form and get which item has been checked avoiding the possibility to select several radio buttons but as soon as I add a name to my radio buttons input then I cannot see in my Model anymore which item is checked. 我想在表单中有两组单选按钮,并检查哪一项以避免避免选择多个单选按钮,但是一旦我在单选按钮输入中添加了名称,便无法在模型中看到项目已检查。

Here is my view: 这是我的看法:

 ...
@using (Html.BeginForm("Validate", "Quiz"))
{
    @for (int i = 0; i < Model.questions.Count(); i++)
    {
          <ul>
                 @{ int j = 0; }
                 @foreach (var ch in Model.questions[i].choices)
                 {
                      <li>
                             @Html.RadioButtonFor(m =>    m.questions[i].choices[j].isChecked, true, new { id = ch.choiceId}) @ch.choiceTitle
                         @Html.HiddenFor(m => m.questions[i].choices[j].isChecked) 
                      </li>
                }
          </ul>
      }

<input type="submit" value="Valider" />

}

And here is my structure: 这是我的结构:

        public class QuizViewModel
        {
            public string quizTitle { get; set; }
            public string quizListDisplay { get; set; }
            public List<QuizQuestion> questions { get; set; }
            public Guid owner { get; set; }
        }

    public class QuizQuestion
    {
        public int questionId { get; set; }
        public int order { get; set; }
        public string questionTitle { get; set; }
        public List<QuizChoice> choices { get; set; }
        public bool isSingleResponseQuestion { get; set; }
    }

    public class QuizChoice
    {
        public int choiceId { get; set; }
        public string choiceTitle { get; set; }
        public int index { get; set; }
        public bool isCorrectAnswer { get; set; }
        public bool isChecked { get; set; }
        public string feedback { get; set; }
        public int selectedAnswer { get; set; }
    }

But the problem is that I can select several radiobuttons in a same group so I added this code to RadioButtonFor after the id: 但是问题是我可以在同一组中选择多个单选按钮,因此我在id之后将以下代码添加到RadioButtonFor中:

@Name = "group" + i @Name =“组” +我

But then I could not get the isChecked in my Model. 但是后来我无法在模型中获取isChecked。 What I am doing wrong here? 我在这里做错了什么? How to resolve this problem? 如何解决这个问题? Thank you Best 最好的谢谢

To resolve the problem I added a field selectedChoiceId to my class QuizQuestion and used the RadioButtonFor as follow: 为了解决该问题,我在类QuizQuestion中添加了一个selectedChoiceId字段,并使用RadioButtonFor如下:

@Html.RadioButtonFor(m => m.questions[i].selectedChoiceId, ch.choiceId) @ Html.RadioButtonFor(m => m.questions [i] .selectedChoiceId,ch.choiceId)

the 1st field of RadioButtonFor serves to group together the radio buttons so it should be the same for all radio buttons for a single question RadioButtonFor的第一个字段用于将单选按钮分组在一起,因此单个问题的所有单选按钮都应该相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM