asp.net mvc 复选框/单选按钮矩阵模型绑定
我有一个带有复选框/单选按钮列表和列表的动态表单。矩阵:
以下代码呈现复选框列表:
@foreach (var sq in Model.SubQuestions)
{
<label>
<input type="hidden" name="answerResult.index" value="@sq.Id" />
<input type="checkbox" name="answerResult[@sq.Id].SubQuestionId" value="@sq.Id" />
@sq.Label.Name
</label>
}
单选按钮列表:
<input type="hidden" name="answerResult.index" value="@Model.Id" />
@foreach (var sq in Model.SubQuestions)
{
<label>
<input type="radio" name="answerResult[@Model.Id].SubQuestionId" value="@sq.Id" />
@sq.Label.Name
</label>
}
控制器中的我的 POST 操作:
[HttpPost]
public ActionResult PassageSurvey(int surveyId, int surveyPageIndex, IList<AnswerResult> answerResult)
其中 IList
是我表单中的自动绑定集合。我只得到已检查/选择的项目。一切都很顺利。
现在我需要从复选框/单选按钮矩阵中获取相同的集合。
单选按钮矩阵:
<table width="100%">
<tr>
<th></th>
@foreach (var av in Model.AnswerVariants)
{
<th style="text-align: center;">
<label>@av.Label.Name</label>
</th>
}
</tr>
@foreach (var sq in Model.SubQuestions)
{
<tr>
<td>
<label>@sq.Label.Name</label>
<input type="hidden" name="answerResult.index" value="@sq.Id" />
<input type="hidden" name="answerResult[@sq.Id].SubQuestionId" value="@sq.Id" />
</td>
@foreach (var av in Model.AnswerVariants)
{
<td align="center">
<input type="radio" name="answerResult[@sq.Id].AnswerVariantId" value="@av.Id" />
</td>
}
</tr>
}
</table>
复选框矩阵:
<table width="100%">
<tr>
<th></th>
@foreach (var av in Model.AnswerVariants)
{
<th style="text-align: center;">
<label>@av.Label.Name</label>
</th>
}
</tr>
@foreach (var sq in Model.SubQuestions)
{
<tr>
<td>
<label>@sq.Label.Name</label>
</td>
@foreach (var av in Model.AnswerVariants)
{
<td align="center">
<input type="hidden" name="answerResult.index" value="@sq.Id" />
<input type="hidden" name="answerResult[@sq.Id].AnswerVariantId" value="@sq.Id" />
<input type="checkbox" name="answerResult[@sq.Id].SubQuestionId" value="@sq.Id" />
</td>
}
</tr>
}
</table>
控制器中的 POST 操作始终相同。 现在,从单选按钮矩阵(在当前示例 3x3 中)IList
始终获取 3 个项目,具体取决于在行和列中选择的项目。 但是从复选框矩阵 (3x3) IList
始终获取所有 9 个项目(无论是否检查了项目,隐藏输入总是有值)
但是我只想获取已检查的项目.我如何更改我的复选框矩阵模板来解决这个问题?
I have a dynamic forms with checkbox/radio-button lists & matrices:
Following code renders checkbox list:
@foreach (var sq in Model.SubQuestions)
{
<label>
<input type="hidden" name="answerResult.index" value="@sq.Id" />
<input type="checkbox" name="answerResult[@sq.Id].SubQuestionId" value="@sq.Id" />
@sq.Label.Name
</label>
}
radio-button list:
<input type="hidden" name="answerResult.index" value="@Model.Id" />
@foreach (var sq in Model.SubQuestions)
{
<label>
<input type="radio" name="answerResult[@Model.Id].SubQuestionId" value="@sq.Id" />
@sq.Label.Name
</label>
}
My POST-action in controller:
[HttpPost]
public ActionResult PassageSurvey(int surveyId, int surveyPageIndex, IList<AnswerResult> answerResult)
where IList<AnswerResult>
is an auto-bound collection from my form. I get only items that were checked/selected. Everything is going well.
Now I need to get the same collection from checkbox/radio-button matrices.
Radio-button matrix:
<table width="100%">
<tr>
<th></th>
@foreach (var av in Model.AnswerVariants)
{
<th style="text-align: center;">
<label>@av.Label.Name</label>
</th>
}
</tr>
@foreach (var sq in Model.SubQuestions)
{
<tr>
<td>
<label>@sq.Label.Name</label>
<input type="hidden" name="answerResult.index" value="@sq.Id" />
<input type="hidden" name="answerResult[@sq.Id].SubQuestionId" value="@sq.Id" />
</td>
@foreach (var av in Model.AnswerVariants)
{
<td align="center">
<input type="radio" name="answerResult[@sq.Id].AnswerVariantId" value="@av.Id" />
</td>
}
</tr>
}
</table>
Checkbox matrix:
<table width="100%">
<tr>
<th></th>
@foreach (var av in Model.AnswerVariants)
{
<th style="text-align: center;">
<label>@av.Label.Name</label>
</th>
}
</tr>
@foreach (var sq in Model.SubQuestions)
{
<tr>
<td>
<label>@sq.Label.Name</label>
</td>
@foreach (var av in Model.AnswerVariants)
{
<td align="center">
<input type="hidden" name="answerResult.index" value="@sq.Id" />
<input type="hidden" name="answerResult[@sq.Id].AnswerVariantId" value="@sq.Id" />
<input type="checkbox" name="answerResult[@sq.Id].SubQuestionId" value="@sq.Id" />
</td>
}
</tr>
}
</table>
POST-action in controller always the same.
Now from radio-button matrix (in current sample 3x3) IList<AnswerResult>
gets always 3 items, depending on items that were selected in rows and columns.
But from Checkbox matrix (3x3) IList<AnswerResult>
gets always all 9 items (regardless items were checked, hidden-inputs always have values)
But I want to get only items, that were checked. How could I change my checkbox-matrix template to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我决定在事后处理这个问题。删除未选中且在
answerResult[##].SubQuestionId
字段中具有null
的项目(复选框)。Finally I decided to handle this problem in my post-action instead. Removing items (checkboxes) that were not selected, and have
nulls
inanswerResult[##].SubQuestionId
fields.