asp.net mvc 复选框/单选按钮矩阵模型绑定

发布于 2024-10-24 19:27:06 字数 3468 浏览 0 评论 0原文

我有一个带有复选框/单选按钮列表和列表的动态表单。矩阵:

checkbox & radio-button Lists

以下代码呈现复选框列表:

@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 是我表单中的自动绑定集合。我只得到已检查/选择的项目。一切都很顺利。

现在我需要从复选框/单选按钮矩阵中获取相同的集合。

checkbox & radio-button matrices

单选按钮矩阵:

<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:

checkbox & radio-button lists

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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2024-10-31 19:27:06

最后我决定在事后处理这个问题。删除未选中且在 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 in answerResult[##].SubQuestionId fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文