ASP.Net 核心 3.1 - 使用 Ajax 将值传递给部分视图模态?

ASP.Net Core 3.1 - passing value to partial view Modal with Ajax?

我想从 table 中删除一条记录,我必须将该记录的 ID 发送到模式,我该怎么做?

主视图:

<tbody>
      @foreach (var item in Model)
         {
           <tr>
                <td> @rowCount</td>
                <td>@item.CategoryTitle</td>
                <td>@item.IconCategory</td>
                <td>
                   <a data-toggle="modal" data-target="#Deletemodal" asp-route- 
                   category="@item.CategoryId" title="delete" class="btn btn-danger btn- 
                   xs">delete</a>
                 </td>
            </tr>
                       
         }
  </tbody>

部分视图作为模式:

@await Html.PartialAsync("_CreateMainCategory")

I want to delete a record from table and I have to send the ID of that record to the modal, how I do this?

为了达到您的要求,您可以尝试在您的模式中动态加载局部视图,如下所示。

<a data-toggle="modal" data-target="#Deletemodal" title="delete" class="btn btn-danger btn-xs" 
   onclick="Func_LoadPv(@item.CategoryId)">delete</a>

Html模态弹窗代码

<div id="Deletemodal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
                @*modal body here*@
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

JS函数

<script>
    function Func_LoadPv(Cid) {
        $.get("/Home/ShowPartialView?CategoryId=" + Cid, function (res) {
            $("div.modal-body").append(res);
        });
    }
</script>

操作方法

public IActionResult ShowPartialView(int CategoryId)
{
    return PartialView("_CreateMainCategory", CategoryId);
}