ASP.NET MVC 5组单选按钮
我正在开始我的第一个ASP.NET MVC项目,所以我有一个简单的问题。我有以下代码:
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>}
Model.GeneralQuestions中的所有问题都是唯一的,因此单选按钮应按名称属性分组(对于每个问题,一组单选按钮)。但是这段代码只生成一个组,所以当我回答第二个问题时,首先会取消选择。我需要改变什么?
编辑
我的模型看起来像:
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; }}
在StudentViewModel的一个实例中,我保存了一个学生和他应该回答的所有问题(一般和他正在研究的科目相关)并将其传递给查看。在视图中,我将所有问题都以单一形式提出,它们都是无线电的类型。那么,任何人都可以帮我分组单选按钮并正确回发此表单吗?