首页 > 解决方案 > .NetCore - 使用 MultipleViews/BindPperty 进行模型绑定

问题描述

我正在尝试在两个属性上使用模型绑定BindProperty,它们都带有一个IList.

我将有X 个单元格,然后对于每个选定的单元格(单元格对象中的布尔值),我有1 Reference. 所以在一个实际的例子中,我有21 个 Cells,其中3个被选中,所以我必须为每个选定的 Cells 选择 1 Reference。直到这里一切都很好,但是当进入我的Post Handler,我存储的IList 正在合并,换句话说,数据存储在同一个 IList 中。因此,当SelectedCells 和 SelectedReferences(见下文)应分别具有 21 和 3 个对象时,它们同时具有 21 个和具有相同名称(例如 Id)的属性被覆盖。

如果视图与两个不同的视图模型相关联,IList 不应该被识别为不同的吗?

[BindProperty]
public IList<CellViewModel> SelectedCells { get; set; }

//Was trying to play with ModelBinder type to force them to be treated as different types, unsuccessfully
//[ModelBinder(BinderType =(typeof(ReferenceViewModel)))]
[BindProperty]
public IList<ReferenceViewModel> SelectedReferences { get; set; }

意见

细胞

@model IList<NoPaper.ViewModels.CellViewModel>


<if include-if="Model.Count == 0">
    <alert type="Warning">
        Não existem células nesta unidade de produção
    </alert>
</if>
<if include-if="Model.Count > 0">

    <div class="list-group list-group-horizontal row">
        @for (int i = 0; i < Model.Count(); i++)
        {
            <if include-if='Model[i].CellCategoryDescription == "Célula Robot" '>
                <div class="list-group-item list-group-item-action text-center col-md-2" style="cursor:pointer;">
                    <input type="hidden" class="cell-value" asp-for="@Model[i].Id" />
                    <input type="hidden" class="cell" asp-for="@Model[i].IsSelected" />
                    @Model[i].Name<text style="font-weight:bold;">(R)</text>
                </div>
            </if>
            <if include-if='Model[i].CellCategoryDescription == "Célula Manual" '>
                <div class="list-group-item list-group-item-action text-center col-md-2" style="cursor:pointer;">
                    <input type="hidden" class="cell-value" asp-for="@Model[i].Id" />
                    <input type="hidden" class="cell" asp-for="@Model[i].IsSelected" />
                    @Model[i].Name<text style="font-weight:bold;">(M)</text>
                </div>
            </if>        
        }
        </div>
</if>

参考

@model IList<NoPaper.ViewModels.ReferenceViewModel>


<if include-if="Model.Count == 0">
    <alert type="Warning">
        Não existem referências nesta unidade de produção
    </alert>
</if>
<if include-if="Model.Count > 0">

    <div class="justify-content-center">
        @for (int i = 0; i < Model.Count(); i++)
        {
            <div class="form-row">
                <div class="form-group ">
                    <text>@Model[i].CellId</text>

                    <label asp-for="@Model[i].myId"></label>
                    <select asp-for="@Model[i].myId" asp-items="Model[i].ReferencesSL" class="custom-select">
                        <option value="">--</option>
                    </select>
                    <span asp-validation-for="@Model[i].myId" class="text-danger"></span>
                </div>
            </div>
        }
    </div>

</if>

标签: asp.net-corerazor-pages

解决方案


您可以尝试将名称属性添加到您的输入中,.net core 使用名称属性绑定数据。例如,如果您要绑定 SelectedCells:

<input type="hidden" class="cell-value" asp-for="@Model[i].Id" name="SelectedCells[@i].Id"/>

如果要绑定 SelectedReferences:

<input type="hidden" class="cell-value" asp-for="@Model[i].Id" name="SelectedReferences[@i].Id"/>

推荐阅读