这里是我的视图模型
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总是假的 - 为什么 - 我失去了什么 - 因为我的复选框提到它工作得很好。 我没有看这个问题, 在这里 ,但我不知道为什么单选按钮需要在视图模型额外的属性,以保持正确的答案时复选框透明地工作。
编辑:我的问题现在模型作为这样
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)
EDIT4:所以最后我得到了一些地方这个作品!
@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 -奇怪