如何在 asp.net 核心中加载 ajax 中的部分视图

how to load partial view in ajax in asp.net core

当我按 ajax.my 更改分页时,我想按行加载我的部分视图部分负责 table 中每一行的按钮 我有如下部分视图:

 @model Guid

<td style="width: 150px">
    <div class="btn-group" role="group">
        <a asp-action="Edit" asp-route-id="@Model" class="btn btn-primary text-white"><i class="fas fa-edit"></i></a>
        <a asp-action="Details" asp-route-id="@Model" class="btn btn-success text-white"><i class="fas fa-list-alt"></i></a>
        <a asp-action="Delete" asp-route-id="@Model" class="btn btn-danger text-white"><i class="fas fa-trash-alt"></i></a>
    </div>
</td>

我在每行 table 中使用它:

   <table id="exp" class="table table-striped border">
                <tr class="table-secondary">
                    <th>
                        @Html.DisplayNameFor(c => c.List.FirstOrDefault().Title)
                    </th>
                    <th></th>
                    <th></th>
                </tr>
                @foreach (var expertise in Model.List)
                {
                    <tr>
                        <td>@Html.DisplayFor(c => expertise.Title)</td>
                        <td>
                            <partial name="_TableButtonPartial" model="@expertise.Id" />
                        </td>
                    </tr>
                }
            </table>
            <div page-model="@Model.PagingInfo" page-classes-enabled="true" page-class="btn border"
                 page-class-normal="btn btn-light" page-class-selected="btn btn-info active"
                 class="btn-group float-right">
            </div>

所以我想进行分页 ajax 但我不知道如何在 ajax

中每行加载这个空间
@section Scripts
{
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    <script type="text/javascript">
        function goToPage(param) {
            var url = '@Url.Content("~/")' + param;

            $.ajax({
                url: url,
                type: 'GET',
                dataType: 'text',
                success: function (data) {
                    var results = JSON.parse(data);
                    var items = '';
                    $('#exp').empty();
                    $.each(results.list,
                        function (i, expertise) {
                            items+= "<tr>" +
                                "<td>" + expertise.title +
                                "</td>" +
                                "<td>" +
                                "<partial name='_TableButtonPartial' model='" +
                                expertise.id +
                                "'/>" +
                                "</td>" +
                                "</tr>";
                        });
                    $('#exp').html(items);
                }
            });
        }
    </script>
}

how to load this partial per row in ajax

您可以修改代码以动态获取 html 部分视图并填充您的 table,如下所示。

$.each(results.list,
    function (i, expertise) {

        //make request to get the html of partial view
        //and generate new rows to populate table

        $.get("/Home/GetPartial/" + expertise.id, function (res) {
            var newrow = "<tr>" +
                "<td>" + expertise.title +
                "</td>" +
                "<td></td>" +
                res +
                "</tr>";

            items += newrow;

            if (i == results.list.length - 1) {
                $('#exp').html(items);
            }

        });
    });

控制器动作

[Route("{controller}/{action}/{id:guid}")]
public IActionResult GetPartial(Guid id)
{
    return PartialView("_TableButtonPartial", id);
}