Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 5 group of radio buttons

Tags:

People also ask

Can you have multiple radio buttons?

Only one radio button in a group can be selected at the same time. Note: The radio group must have share the same name (the value of the name attribute) to be treated as a group.

How many radio buttons you can select at a time from one group?

Only one radio button in a given group can be selected at the same time. Radio buttons are typically rendered as small circles, which are filled or highlighted when selected.

Which attribute is used to make a group of radio buttons?

We can put different radio buttons in a group using the name attribute. All the buttons in a group must have the same name attribute.


I am starting my first ASP.NET MVC project, so I have one simple question. I have following code:

foreach(var question in Model.GeneralQuestions) {     <div class = "well">         <h3>             <strong>@question.QuestionString</strong>         </h3>         @foreach (var answer in question.PossibleAnswers)         {             @Html.RadioButtonFor(model => question.QuestionString, answer.Answer)             @Html.Label(answer.Answer)             <br />         }     </div> } 

All questions in Model.GeneralQuestions are unique, so radio buttons should be divided into groups by name attribute (for each question one group of radio buttons). But this code produces only one group, so when I answer second question first one becomes deselected. What do I need to change?

EDIT
My model looks like:

public class StudentViewModel {     public Student Student { get; set; }     public List<Question> GeneralQuestions { get; set; }     public List<SubjectQuestions> SubjectQuestions { get; set; } } public class Student {     public int StudentID { get; set; }     public string Index { get; set; }     public string Name { get; set; }     public string Surname { get; set; }      public virtual ICollection<Subject> Subjects { get; set; } } public class Question {     public int QuestionID { get; set; }     public string QuestionString { get; set; }     public bool IsAssociatedWithSubject { get; set; }      public virtual ICollection<PossibleAnswer> PossibleAnswers { get; set; }     public virtual ICollection<Results> Results { get; set; } } public class SubjectQuestions {     public Subject Subject { get; set; }     public List<Question> Questions { get; set; } } public class Results {     public int ResultsID { get; set; }     public int QuestionID { get; set; }     public int? SubjectID { get; set; }     public int PossibleAnswerID { get; set; }      public virtual Question Question { get; set; }     public virtual PossibleAnswer PossibleAnswer { get; set; }     public virtual Subject Subject { get; set; } } 

In one instance of StudentViewModel I save one student and all questions that he should answer (both general and related to subjects he is studying) and pass it to view. In view I put all questions in single form and they are all type of radio. So, can anyone help me with grouping of radio buttons and posting back this form correctly?