.Net Core - 模型绑定 MultipleViews/BindPoperty
.NetCore - Model Binding with MultipleViews/BindPoperty
我正在尝试在两个属性上使用 模型绑定,它们都具有 BindProperty
到 IList
。
我将有 X 个单元格,然后对于每个 selected 单元格(单元格对象中的 bool),我有 1 Reference
。因此,在一个实际示例中,我有 21 个单元格,其中 3 个是 selected,所以我必须 select 1 个 selected 单元格 的参考。到这里为止一切都很好,但是当进入我的 Post Handler 时,我存储的 ILists 正在合并,换句话说,数据存储在同一个 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>
您可以尝试将名称属性添加到您的输入中,.net core 绑定数据名称 attribute.For 例如,如果您想绑定 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"/>
我正在尝试在两个属性上使用 模型绑定,它们都具有 BindProperty
到 IList
。
我将有 X 个单元格,然后对于每个 selected 单元格(单元格对象中的 bool),我有 1 Reference
。因此,在一个实际示例中,我有 21 个单元格,其中 3 个是 selected,所以我必须 select 1 个 selected 单元格 的参考。到这里为止一切都很好,但是当进入我的 Post Handler 时,我存储的 ILists 正在合并,换句话说,数据存储在同一个 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>
您可以尝试将名称属性添加到您的输入中,.net core 绑定数据名称 attribute.For 例如,如果您想绑定 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"/>