我正在开始我的第一个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的一个实例中,我保存了一个学生和他应该回答的所有问题(包括一般问题和与他正在学习的科目相关的问题),并将其传递给查看。有鉴于此,我把所有的问题都放在一个单一的形式,它们都是类型的广播。那么,有没有人可以帮我对单选按钮进行分组并正确地发回这个表单呢?
https://stackoverflow.com/questions/28055287
复制相似问题