2016-12-27 7 views
0

フォームにラジオボタンの2つのグループがあり、いくつかのラジオボタンを選択する可能性を避けて、私のラジオボタンの入力には、私のモデルでは、どの項目がチェックされているかわかりません。ここで@ Html.RadioButton単一の選択で選択されたオプションを取得する

が私の見解です:

... 
@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" /> 

} 

そして、ここでは私の構造体である:

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

しかし、問題は、私はRadioButtonForにこのコードを追加したので、私は同じグループ内のいくつかのラジオボタンを選択することができるということですafter:id:

@Name = "group" + i

しかし、その後私は私のモデルでisCheckedを取得できませんでした。 私はここで間違っていますか?この問題を解決するには? は、私は私のクラスQuizQuestionにフィールドselectedChoiceIdを追加し、次のようにRadioButtonForを使用し、問題を解決するには、あなたに ベスト

答えて

0

ありがとう:

Html.RadioButtonFor(M => m.questions @ [i]の.selectedChoiceIdは、 ch.choiceId)

RadioButtonForの1番目のフィールドはラジオボタンをまとめてグループ化するため、1つの質問のすべてのラジオボタンで同じにする必要があります

関連する問題