我開始了我的第一個ASP.NET MVC項目,所以我有一個簡單的問題。 我有以下代碼:ASP.NET MVC 5組單選按鈕
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所有問題都是獨一無二的,所以單選按鈕應該由name屬性被分成組(每題一組單選按鈕)。但是這個代碼只產生一個組,所以當我第一個回答第二個問題時就會取消選擇。 我需要改變什麼?
編輯
我的模型看起來像:
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的一個實例我救一個學生,所有的問題,他應該回答(一般和相關學科他是學)和通它來查看。鑑於我將所有問題都以單一形式提出,並且都是無線電廣播。所以,任何人都可以幫助我分組單選按鈕並正確回覆此表單嗎?
只是一個快速反應,但你見過這篇文章嗎? http://stackoverflow.com/a/22178728/1765853 – macoms01 2015-01-20 21:22:27
只要'QuestionString'是唯一的,這應該是爲每個問題創建一個組,雖然綁定到'QuestionString'似乎是固定的 - 你不應該綁定到類似的東西'SelectedAnswer'?你可以顯示一些這是生成的HTML – 2015-01-20 21:27:39
你的外循環也應該是一個'for'循環,這樣你的控件屬性可以通過索引器命名。 – 2015-01-20 21:30:50