我在mvc 5上做了一个反馈表,反馈表由10个问题组成,每个问题有5个选定答案。这些选定的答案是我填充的单选按钮组。但是在post action方法中,单选按钮没有绑定。以下是我的代码。
Model.cs
public class FeedbackForm
{
public string Id { get; set; }
public Models.Questionnaire mquestions { get; set; }
public List<Questionnaire> Question { get; set; }
public Batches batches { get; set; }
public User user { get; set; }
public DateTime Datetime { get; set; }
}
public class Questionnaire
{
public string QuestionId { get; set; }
public string QuestionTitle { get; set; }
[Required]
public int? SelectedAnswer { get; set; } // for binding
public IEnumerable<QuestionnaireStatus> PossibleAnswers { get; set; }
}
public class QuestionnaireStatus
{
public string StatusId { get; set; }
public string StatusTitle { get; set; }
}
Controller.cs
public ActionResult Index()
{
List<Models.FeedbackForm> lstfeedback = new List<Models.FeedbackForm>();
Models.FeedbackForm form = new Models.FeedbackForm();
Context.FeedBackForm contFeedback = new Context.FeedBackForm();
Models.FeedbackForm vm = new Models.FeedbackForm();
form.Question = new List<Models.Questionnaire>();
form.Question = contFeedback.GetAllQuestionnaire();
for (int i = 0; i < form.Question.Count; i++)
{
lstfeedback.Add(new Models.FeedbackForm
{
Question = new List<Models.Questionnaire>()
{ new Models.Questionnaire
{
QuestionId = form.Question[i].QuestionId,
QuestionTitle = form.Question[i].QuestionTitle
,
PossibleAnswers = contFeedback.GetAllStatus()
},
}
});
}
return View(lstfeedback);
}
View.cshtml
@using (Html.BeginForm())
{
<table class="table">
<tr>
<th>
@*@Html.Display("Questions")*@
</th>
@for (int i = 0; i < Model.Count; i++)
{
for (int j = 0; j < Model[i].Question.Count; j++)
{
foreach (var item in Model[i].Question[j].PossibleAnswers)
{
<th>
@item.StatusTitle
</th>
}
break;
}
break;
}
</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
@for (int j = 0; j < Model[i].Question.Count; j++)
{
<td>
@Html.DisplayFor(m=>Model[i].Question[j].QuestionTitle)
</td>
foreach (var item in Model[i].Question[j].PossibleAnswers)
{
<td>
@Html.RadioButtonFor(m => Model[i].Question[j].SelectedAnswer, item.StatusId, new { id = item.StatusId })
</td>
}
@Html.ValidationMessageFor(m => Model[i].Question[j].SelectedAnswer)
}
</tr>
}
</table>
<div class="form-group">
<div class="col-md-10">
<input type="submit" id="Attendance" value="Submit" class="btn btn-default" />
</div>
</div>
}