部分视图无法使用动态 ID 正确呈现?
Partial View Does not render correctly with dynamic id?
我正在尝试呈现部分视图(可重复使用)以便删除 table(客户列表)中具有 cus_id 的客户
但是每次我提交表单时只有第一行id提交请告诉我正确的解决方案。
@foreach (var item in Model)
{
....
<td>
@await Html.PartialAsync("~/Views/Shared/Modal.cshtml", item.cus_id)
@*<partial name="~/Views/Shared/Modal.cshtml" for="@item.cus_id"/>*@
</td>
}
这是我的看法
@model int
<span>
<a href="#/" data-toggle="modal" data-target="#myModal"><i class="fas fa-trash-alt" title="Delete"></i></a>
</span>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header ">
<h4 class="modal-title">Warning</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<form asp-action="Delete" asp-route-id="@Model" method="post">
<span>
<span><b>Are you sure you want to delete?</b></span>
<button type="submit" class="btn btn-danger">Yes</button>
<a href="#/" class="btn btn-primary" data-dismiss="modal">No</a>
</span>
</form>
</div>
</div>
</div>
</div>
问题出在你的局部视图上。
<a href="#/" data-toggle="modal" data-target="#myModel"><i class="fas fa-trash-alt" title="Delete"></i></a>
您使用了 ID select或者,它将始终 select 第一个 ID 为 "myModel" 的模态。虽然所有模态的 ID 都相同。所以,每次提交,只有第一行id在提交。
您可以将 cus_id 应用于模态 id 以区分应打开哪个模态。
<span>
<a href="#/" data-toggle="modal" data-target="#myModal_@Model"><i class="fas fa-trash-alt" title="Delete"></i></a>
</span>
<div id="myModal_@Model" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header ">
<h4 class="modal-title">Warning</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<form asp-action="Delete" asp-route-id="@Model" method="post">
<span>
<span><b>Are you sure you want to delete @Model?</b></span>
<button type="submit" class="btn btn-danger">Yes</button>
<a href="#/" class="btn btn-primary" data-dismiss="modal">No</a>
</span>
</form>
</div>
</div>
</div>
我正在尝试呈现部分视图(可重复使用)以便删除 table(客户列表)中具有 cus_id 的客户 但是每次我提交表单时只有第一行id提交请告诉我正确的解决方案。
@foreach (var item in Model)
{
....
<td>
@await Html.PartialAsync("~/Views/Shared/Modal.cshtml", item.cus_id)
@*<partial name="~/Views/Shared/Modal.cshtml" for="@item.cus_id"/>*@
</td>
}
这是我的看法
@model int
<span>
<a href="#/" data-toggle="modal" data-target="#myModal"><i class="fas fa-trash-alt" title="Delete"></i></a>
</span>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header ">
<h4 class="modal-title">Warning</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<form asp-action="Delete" asp-route-id="@Model" method="post">
<span>
<span><b>Are you sure you want to delete?</b></span>
<button type="submit" class="btn btn-danger">Yes</button>
<a href="#/" class="btn btn-primary" data-dismiss="modal">No</a>
</span>
</form>
</div>
</div>
</div>
</div>
问题出在你的局部视图上。
<a href="#/" data-toggle="modal" data-target="#myModel"><i class="fas fa-trash-alt" title="Delete"></i></a>
您使用了 ID select或者,它将始终 select 第一个 ID 为 "myModel" 的模态。虽然所有模态的 ID 都相同。所以,每次提交,只有第一行id在提交。
您可以将 cus_id 应用于模态 id 以区分应打开哪个模态。
<span>
<a href="#/" data-toggle="modal" data-target="#myModal_@Model"><i class="fas fa-trash-alt" title="Delete"></i></a>
</span>
<div id="myModal_@Model" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header ">
<h4 class="modal-title">Warning</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<form asp-action="Delete" asp-route-id="@Model" method="post">
<span>
<span><b>Are you sure you want to delete @Model?</b></span>
<button type="submit" class="btn btn-danger">Yes</button>
<a href="#/" class="btn btn-primary" data-dismiss="modal">No</a>
</span>
</form>
</div>
</div>
</div>