這裏是我的視圖模型@html剃刀單選按鈕mvc5
public class UserResponseModel
{
public string QuestionId { get; set;}
public string QuestionText { get; set; }
public bool IsChecked { get; set; }
}
所以,對於複選框這個精美的作品
for (var i = 0; i < Model.UserResponses.Count; i++)
{
<div class="row">
<div class="col-md-4">
<div class="form-group">
@Html.HiddenFor(x => x.UserResponses[i].QuestionId)
@Html.CheckBoxFor(x => x.UserResponses[i].IsChecked)
但單選按鈕,這並不
for (var i = 0; i < Model.UserResponses.Count; i++)
{
<div class="row">
<div class="col-md-4">
<div class="form-group">
@Html.HiddenFor(x => x.UserResponses[i].QuestionId)
@Html.RadioButtonFor(x => x.UserResponses[i].IsChecked, new { Name = "grp1" })
@Html.DisplayTextFor(x => x.UserResponses[i].QuestionText)
當我提交表單然後IsChecked總是假的 - 爲什麼 - 我缺少什麼 - 正如我提到的複選框,它工作得很好。我看過這個問題here但我不確定爲什麼單選按鈕在視圖模型中需要額外的屬性才能在複選框透明工作時保持正確的答案。
編輯:我的問題模型現在既然這麼
public class QuestionModel
{
public string WhichQuestion { get; set; }
public int PointsObtained { get; set; }
public bool CorrectAnswer { get; set; }
private List<UserResponseModel> _userResponse;
public List<UserResponseModel> UserResponses
{
get { return _userResponse ?? (_userResponse = new List<UserResponseModel>()); }
set { _userResponse = value; }
}
}
通知剛纔說我公佈爾CorrectAnswer {獲得;組; }
在我看來,這裏的代碼
for (var i = 0; i < Model.UserResponses.Count; i++)
{
<div class="row">
<div class="col-md-4">
<div class="form-group">
@Html.HiddenFor(x => x.UserResponses[i].QuestionId)
@Html.HiddenFor(x => x.CorrectAnswer)
@Html.HiddenFor(x => x.UserResponses[i].IsChecked)
@Html.RadioButtonFor(x => x.UserResponses[i].IsChecked, Model.CorrectAnswer, new { Name = "grp1" })
編輯2:
@Html.HiddenFor(x => x.UserResponses[i].QuestionId)
@Html.HiddenFor(x => x.SelectedAnswerId)
@Html.HiddenFor(x => x.UserResponses[i].IsChecked)
@Html.RadioButtonFor(x => x.UserResponses[i].IsChecked, Model.SelectedAnswerId, new { Name = "grp1" })
EDIT3:
public class QuestionModel
{
public string WhichQuestion { get; set; }
public int PointsObtained { get; set; }
private List<UserResponseModel> _userResponse;
public List<UserResponseModel> UserResponses
{
get { return _userResponse ?? (_userResponse = new List<UserResponseModel>()); }
set { _userResponse = value; }
}
}
public class UserResponseModel
{
public string QuestionId { get; set;}
public string QuestionText { get; set; }
public string SelectedQuestionId { get; set; }
}
終於,我的觀點
for (var i = 0; i < Model.UserResponses.Count; i++)
{
<div class="row">
<div class="col-md-4">
<div class="form-group">
@Html.HiddenFor(x => x.UserResponses[i].QuestionId)
@Html.RadioButtonFor(x => x.UserResponses[i].SelectedQuestionId, Model.UserResponses[i].QuestionId, new { Name = "grp1" })
@*@Html.CheckBoxFor(x => x.UserResponses[i].IsChecked)*@
@Html.DisplayTextFor(x => x.UserResponses[i].QuestionText)
編輯4:所以最後我得到一些 這個作品!
@Html.RadioButtonFor(x => x.UserResponses[i].SelectedQuestionId, Model.UserResponses[i].QuestionId)
我現在可以看到selectedquestionid填充在我httppost方法,但如果我這樣做
@Html.RadioButtonFor(x => x.UserResponses[i].SelectedQuestionId, Model.UserResponses[i].QuestionId, new {Name="grp"})
然後我雖然能只有一個選擇單選按鈕的selectedquestionid 是空上httppost - 奇怪
這可能會幫助你http://www.techiesweb。net/radio-button-list-in-asp-net-mvc/ – Shyju
@Shyju看到我的edit4 – usr28765526