部分视图无法使用动态ID正确呈现?

问题描述 投票:0回答:1

我正在尝试渲染部分视图(可重用),以便删除表(客户列表)中具有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">&times;</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>
c# asp.net-mvc asp.net-core asp.net-core-mvc razorengine
1个回答
0
投票
问题出在您的局部视图中。

<a href="#/" data-toggle="modal" data-target="#myModel"><i class="fas fa-trash-alt" title="Delete"></i></a>

您使用了ID选择器,它将始终选择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">&times;</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>

© www.soinside.com 2019 - 2024. All rights reserved.