Pagination 如何在asp.net核心Web API 2.1中实现服务器端分页

Pagination 如何在asp.net核心Web API 2.1中实现服务器端分页,pagination,asp.net-core-webapi,Pagination,Asp.net Core Webapi,我想在表中实现服务器端分页。我需要知道在API控制器get方法中要编写什么代码以及任何其他需要的代码 我试图在网上搜索,但找不到任何资源。任何接近的资源都无法解释 有关我想要的示例,请参考以下链接(仅查看页码): 基本上,我想在底部的页码,当你点击他们的页面变化 据我所知,应该发生的是,当我向API发送一个HTTP请求时,URL中有页码、页数限制,它应该返回结果,结果数等于页数限制,并且根据页码它应该跳过一些结果。 此外,生成的json应该包含页面数,以便我可以填充表下的页面数 它应该类似于以

我想在表中实现服务器端分页。我需要知道在API控制器get方法中要编写什么代码以及任何其他需要的代码

我试图在网上搜索,但找不到任何资源。任何接近的资源都无法解释

有关我想要的示例,请参考以下链接(仅查看页码):

基本上,我想在底部的页码,当你点击他们的页面变化

据我所知,应该发生的是,当我向API发送一个HTTP请求时,URL中有页码、页数限制,它应该返回结果,结果数等于页数限制,并且根据页码它应该跳过一些结果。 此外,生成的json应该包含页面数,以便我可以填充表下的页面数

它应该类似于以下内容,除了以下内容是在node中编写的,并且使用mongodb

我想使用.net核心web API和SQL Server

当前代码:

API控制器

 [Route("api/customer")]
public class CustomerController : Controller
{
    private readonly InventoryDatabaseContext _context;

    public CustomerController(InventoryDatabaseContext context)
    {
        _context = context;

    }

    GET: api/<controller>
    [HttpGet]
    public ActionResult<List<CustomerTable>> GetAll()
    {
        return _context.CustomerTable.ToList();
    }
}
[路线(“api/客户”)]
公共类CustomerController:控制器
{
私有只读InventoryDatabaseContext\u上下文;
公共CustomerController(InventoryDatabaseContext上下文)
{
_上下文=上下文;
}
获取:api/
[HttpGet]
公共行动结果GetAll()
{
return_context.CustomerTable.ToList();
}
}
模型

名称空间API.Models
{
公共部分类CustomerTable
{
公共静态列表客户=新列表();
[关键]
public int CustomerId{get;set;}
公共字符串CustomerName{get;set;}
公共字符串CustomerTelephone{get;set;}
公共字符串CustomerAddress{get;set;}
公共字符串CustomerEmail{get;set;}
}
}
HTML和javascript

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">


</head>
<body>
    <style>
        .selected {
            background-color: red;
        }
    </style>
    <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal2" type="button">Add</button>
    <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" type="button" id="editbtn" onclick="editItem(rowid)">Edit</button>
    <button class="btn btn-primary btn-lg" type="button" onclick="deleteItem(rowid)">Delete</button>

    <div>
        <table id="datatable" class="table table-hover table-bordered table-striped">
            <thead>
                <tr>
                    <th>Customer Name</th>
                    <th>Customer Telephone</th>
                    <th>Customer Address</th>
                    <th>Customer Email</th>
                </tr>
            </thead>
            <tbody id="cus"></tbody>
        </table>
        <ul id="pagination-demo" class="pagination-lg pull-right"></ul>
    </div>

    <!-- The Modal -->
    <div class="modal" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- Modal Header -->
                <div class="modal-header">
                    <h4 class="modal-title">Modal Heading</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>

                <!-- Modal body -->
                <div class="modal-body">
                    <form class="my-form">
                        <!--<input type="hidden" id="edit-id">-->
                        <input class="form-control" type="hidden" id="edit-id">
                        <div class="form-group row">
                            <label for="edit-name" class="col-2 col-form-label">Customer Name</label>
                            <div class="col-5">
                                <input class="form-control" type="text" id="edit-name" placeholder="Enter Customer Name">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="edit-telephone" class="col-2 col-form-label">Customer Telephone</label>
                            <div class="col-5">
                                <input class="form-control" type="text" id="edit-telephone" placeholder="Enter Customer Telephone">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="edit-address" class="col-2 col-form-label">Customer Address</label>
                            <div class="col-5">
                                <input class="form-control" type="text" id="edit-address" placeholder="Enter Customer Address">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="edit-email" class="col-2 col-form-label">Customer Email</label>
                            <div class="col-5">
                                <input class="form-control" type="text" id="edit-email" placeholder="Enter Customer Email">
                            </div>
                        </div>
                        <!--<input type="submit" value="Edit">-->
                        <button type="button" class="btn btn-primary btn-lg" onclick="editcustomer()">Edit</button>
                        <!--<a onclick="closeInput()" aria-label="Close">&#10006;</a>-->
                    </form>
                </div>

                <!-- Modal footer -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>

            </div>
        </div>
    </div>
    <!-- The Modal2 -->
    <div class="modal" id="myModal2">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- Modal Header -->
                <div class="modal-header">
                    <h4 class="modal-title">Modal Heading</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>

                <!-- Modal body -->
                <div class="modal-body">
                    <form>
                        <div class="form-group row">
                            <label for="add-name" class="col-2 col-form-label">Customer Name</label>
                            <div class="col-5">
                                <input class="form-control" type="text" id="add-name" placeholder="Enter Customer Name">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="add-telephone" class="col-2 col-form-label">Customer Telephone</label>
                            <div class="col-5">
                                <input class="form-control" type="text" id="add-telephone" placeholder="Enter Customer Telephone">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="add-address" class="col-2 col-form-label">Customer Address</label>
                            <div class="col-5">
                                <input class="form-control" type="text" id="add-address" placeholder="Enter Customer Address">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="add-email" class="col-2 col-form-label">Customer Email</label>
                            <div class="col-5">
                                <input class="form-control" type="text" id="add-email" placeholder="Enter Customer Email">
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-2">
                                <button type="button" class="btn btn-primary btn-lg" onclick="addCustomer()">Add</button>
                            </div>
                        </div>
                    </form>
                </div>

                <!-- Modal footer -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>

            </div>
        </div>
    </div>
    <input id="startDate" type="text" />


    <script src="~/AdminLTE/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="https://www.solodev.com/assets/pagination/jquery.twbsPagination.js"></script>
    <script>

        var editor;



        let cus = null;
        $(document).ready(function () {
            getCustomer();
            getrow();
            highlightrow();
            $("button[id=editbtn]").attr("disabled", "disabled");
            $("#startDate").ejDatePicker();


            $('#pagination-demo').twbsPagination({
                totalPages: 5,
                // the current page that show on start
                startPage: 1,

                // maximum visible pages
                visiblePages: 5,

                initiateStartPageClick: true,

                // template for pagination links
                href: false,

                // variable name in href template for page number
                hrefVariable: '{{number}}',

                // Text labels
                first: 'First',
                prev: 'Previous',
                next: 'Next',
                last: 'Last',

                // carousel-style pagination
                loop: false,

                // callback function
                onPageClick: function (event, page) {
                    $('.page-active').removeClass('page-active');
                    $('#page' + page).addClass('page-active');
                },

                // pagination Classes
                paginationClass: 'pagination',
                nextClass: 'next',
                prevClass: 'prev',
                lastClass: 'last',
                firstClass: 'first',
                pageClass: 'page',
                activeClass: 'active',
                disabledClass: 'disabled'

            });



        });
        $('#datatable').on('click', 'tr', function () {
            $(this).addClass('info').siblings().removeClass('info');
            $("button[id=editbtn]").removeAttr("disabled");
        });
        function highlightrow() {

        }
        function getCustomer() {
            var json_url = 'https://localhost:44336/api/customer';
            $.ajax({
                url: json_url,
                type: 'GET',


                success: function (data) {
                    $('#cus').empty();
                    $.each(data, function (index, element) {

                        $('#cus').append('<tr id="high"><td style="display:none;">' + element.customerId + '</td>' +
                            '<td> ' + element.customerName + '</td > ' +
                            '<td>' + element.customerTelephone + '</td>' +
                            '<td>' + element.customerAddress + '</td>' +
                            '<td>' + element.customerEmail + '</td>'
                        );
                    });
                    cus = data;

                }
            });
        }
        var rowid;
        function getrow() {
            var table = document.getElementsByTagName("table")[0];
            var tbody = table.getElementsByTagName("tbody")[0];
            tbody.onclick = function (e) {
                e = e || window.event;
                var data = [];
                var target = e.srcElement || e.target;
                while (target && target.nodeName !== "TR") {
                    target = target.parentNode;
                }
                if (target) {
                    var cells = target.getElementsByTagName("td");
                    data.push(cells[0].innerHTML);
                }

                rowid = data
            };
        }

        function addCustomer() {
            uri = 'https://localhost:44336/api/customer';
            var itemObj = {
                'CustomerName': $('#add-name').val(),
                'CustomerTelephone': $('#add-telephone').val(),
                'CustomerAddress': $('#add-address').val(),
                'CustomerEmail': $('#add-email').val()
            };

            $.ajax({
                type: 'POST',
                url: uri,
                data: JSON.stringify(itemObj),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (result) {
                    getCustomer();
                    $('#add-name').val('');
                    $('#add-telephone').val('');
                    $('#add-address').val('');
                    $('#add-email').val('');
                }
            });
        }

        function deleteItem(rowid) {
            uri = 'https://localhost:44336/api/customer';
            $.ajax({
                url: uri + '/' + rowid,
                type: 'DELETE',
                success: function (result) {
                    getCustomer()
                }
            });

        }

        function editItem(rowid) {
            $.each(cus, function (key, item) {
                if (item.customerId == rowid) {
                    $('#edit-id').val(item.customerId);
                    $('#edit-name').val(item.customerName);
                    $('#edit-telephone').val(item.customerTelephone);
                    $('#edit-address').val(item.customerAddress);
                    $('#edit-email').val(item.customerEmail);
                }
            });
        }

        function editcustomer() {
            $("#spoilertwo").show();
            const item = {
                'customerName': $('#edit-name').val(),
                'customerTelephone': $('#edit-telephone').val(),
                'customerAddress': $('#edit-address').val(),
                'customerEmail': $('#edit-email').val()

            };

            $.ajax({
                url: 'https://localhost:44336/api/customer/' + rowid,
                type: 'PUT',
                accepts: 'application/json',
                contentType: 'application/json',
                data: JSON.stringify(item),
                success: function (result) {
                    getCustomer();
                    $('#edit-name').val('');
                    $('#edit-telephone').val('');
                    $('#edit-address').val('');
                    $('#edit-email').val('');
                }
            });

            $("#spoiler").hide();

            return false;
        };

    </script>
</body>
</html>

.选定{
背景色:红色;
}
添加
编辑
删除
客户名称
客户电话
客户地址
客户电子邮件
    模态标题 &时代; 客户名称 客户电话 客户地址 客户电子邮件 编辑 接近 模态标题 &时代; 客户名称 客户电话 客户地址 客户电子邮件 添加 接近 变量编辑器; 设cus=null; $(文档).ready(函数(){ getCustomer(); getrow(); highlightrow(); $(“按钮[id=editbtn]”)attr(“已禁用”、“已禁用”); $(“#开始日期”).ejDatePicker(); $(“#分页演示”).twb分页({ 总页数:5页, //开始时显示的当前页面 起始页:1, //最大可见页面数 浏览网页:5, initiateStartPageClick:true, //分页链接的模板 href:false, //href模板中页码的变量名 hrefVariable:“{{number}}”, //文本标签 第一:"第一",, 上一篇:'上一篇', 下一个:'下一个', last:‘last’, //转盘式分页 循环:false, //回调函数 onPageClick:功能(事件,页面){ $('.page-active').removeClass('page-active');
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    
    
    </head>
    <body>
        <style>
            .selected {
                background-color: red;
            }
        </style>
        <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal2" type="button">Add</button>
        <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" type="button" id="editbtn" onclick="editItem(rowid)">Edit</button>
        <button class="btn btn-primary btn-lg" type="button" onclick="deleteItem(rowid)">Delete</button>
    
        <div>
            <table id="datatable" class="table table-hover table-bordered table-striped">
                <thead>
                    <tr>
                        <th>Customer Name</th>
                        <th>Customer Telephone</th>
                        <th>Customer Address</th>
                        <th>Customer Email</th>
                    </tr>
                </thead>
                <tbody id="cus"></tbody>
            </table>
            <ul id="pagination-demo" class="pagination-lg pull-right"></ul>
        </div>
    
        <!-- The Modal -->
        <div class="modal" id="myModal">
            <div class="modal-dialog">
                <div class="modal-content">
    
                    <!-- Modal Header -->
                    <div class="modal-header">
                        <h4 class="modal-title">Modal Heading</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
    
                    <!-- Modal body -->
                    <div class="modal-body">
                        <form class="my-form">
                            <!--<input type="hidden" id="edit-id">-->
                            <input class="form-control" type="hidden" id="edit-id">
                            <div class="form-group row">
                                <label for="edit-name" class="col-2 col-form-label">Customer Name</label>
                                <div class="col-5">
                                    <input class="form-control" type="text" id="edit-name" placeholder="Enter Customer Name">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="edit-telephone" class="col-2 col-form-label">Customer Telephone</label>
                                <div class="col-5">
                                    <input class="form-control" type="text" id="edit-telephone" placeholder="Enter Customer Telephone">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="edit-address" class="col-2 col-form-label">Customer Address</label>
                                <div class="col-5">
                                    <input class="form-control" type="text" id="edit-address" placeholder="Enter Customer Address">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="edit-email" class="col-2 col-form-label">Customer Email</label>
                                <div class="col-5">
                                    <input class="form-control" type="text" id="edit-email" placeholder="Enter Customer Email">
                                </div>
                            </div>
                            <!--<input type="submit" value="Edit">-->
                            <button type="button" class="btn btn-primary btn-lg" onclick="editcustomer()">Edit</button>
                            <!--<a onclick="closeInput()" aria-label="Close">&#10006;</a>-->
                        </form>
                    </div>
    
                    <!-- Modal footer -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    </div>
    
                </div>
            </div>
        </div>
        <!-- The Modal2 -->
        <div class="modal" id="myModal2">
            <div class="modal-dialog">
                <div class="modal-content">
    
                    <!-- Modal Header -->
                    <div class="modal-header">
                        <h4 class="modal-title">Modal Heading</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
    
                    <!-- Modal body -->
                    <div class="modal-body">
                        <form>
                            <div class="form-group row">
                                <label for="add-name" class="col-2 col-form-label">Customer Name</label>
                                <div class="col-5">
                                    <input class="form-control" type="text" id="add-name" placeholder="Enter Customer Name">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="add-telephone" class="col-2 col-form-label">Customer Telephone</label>
                                <div class="col-5">
                                    <input class="form-control" type="text" id="add-telephone" placeholder="Enter Customer Telephone">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="add-address" class="col-2 col-form-label">Customer Address</label>
                                <div class="col-5">
                                    <input class="form-control" type="text" id="add-address" placeholder="Enter Customer Address">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="add-email" class="col-2 col-form-label">Customer Email</label>
                                <div class="col-5">
                                    <input class="form-control" type="text" id="add-email" placeholder="Enter Customer Email">
                                </div>
                            </div>
                            <div class="form-group row">
                                <div class="col-2">
                                    <button type="button" class="btn btn-primary btn-lg" onclick="addCustomer()">Add</button>
                                </div>
                            </div>
                        </form>
                    </div>
    
                    <!-- Modal footer -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    </div>
    
                </div>
            </div>
        </div>
        <input id="startDate" type="text" />
    
    
        <script src="~/AdminLTE/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
        <script src="https://www.solodev.com/assets/pagination/jquery.twbsPagination.js"></script>
        <script>
    
            var editor;
    
    
    
            let cus = null;
            $(document).ready(function () {
                getCustomer();
                getrow();
                highlightrow();
                $("button[id=editbtn]").attr("disabled", "disabled");
                $("#startDate").ejDatePicker();
    
    
                $('#pagination-demo').twbsPagination({
                    totalPages: 5,
                    // the current page that show on start
                    startPage: 1,
    
                    // maximum visible pages
                    visiblePages: 5,
    
                    initiateStartPageClick: true,
    
                    // template for pagination links
                    href: false,
    
                    // variable name in href template for page number
                    hrefVariable: '{{number}}',
    
                    // Text labels
                    first: 'First',
                    prev: 'Previous',
                    next: 'Next',
                    last: 'Last',
    
                    // carousel-style pagination
                    loop: false,
    
                    // callback function
                    onPageClick: function (event, page) {
                        $('.page-active').removeClass('page-active');
                        $('#page' + page).addClass('page-active');
                    },
    
                    // pagination Classes
                    paginationClass: 'pagination',
                    nextClass: 'next',
                    prevClass: 'prev',
                    lastClass: 'last',
                    firstClass: 'first',
                    pageClass: 'page',
                    activeClass: 'active',
                    disabledClass: 'disabled'
    
                });
    
    
    
            });
            $('#datatable').on('click', 'tr', function () {
                $(this).addClass('info').siblings().removeClass('info');
                $("button[id=editbtn]").removeAttr("disabled");
            });
            function highlightrow() {
    
            }
            function getCustomer() {
                var json_url = 'https://localhost:44336/api/customer';
                $.ajax({
                    url: json_url,
                    type: 'GET',
    
    
                    success: function (data) {
                        $('#cus').empty();
                        $.each(data, function (index, element) {
    
                            $('#cus').append('<tr id="high"><td style="display:none;">' + element.customerId + '</td>' +
                                '<td> ' + element.customerName + '</td > ' +
                                '<td>' + element.customerTelephone + '</td>' +
                                '<td>' + element.customerAddress + '</td>' +
                                '<td>' + element.customerEmail + '</td>'
                            );
                        });
                        cus = data;
    
                    }
                });
            }
            var rowid;
            function getrow() {
                var table = document.getElementsByTagName("table")[0];
                var tbody = table.getElementsByTagName("tbody")[0];
                tbody.onclick = function (e) {
                    e = e || window.event;
                    var data = [];
                    var target = e.srcElement || e.target;
                    while (target && target.nodeName !== "TR") {
                        target = target.parentNode;
                    }
                    if (target) {
                        var cells = target.getElementsByTagName("td");
                        data.push(cells[0].innerHTML);
                    }
    
                    rowid = data
                };
            }
    
            function addCustomer() {
                uri = 'https://localhost:44336/api/customer';
                var itemObj = {
                    'CustomerName': $('#add-name').val(),
                    'CustomerTelephone': $('#add-telephone').val(),
                    'CustomerAddress': $('#add-address').val(),
                    'CustomerEmail': $('#add-email').val()
                };
    
                $.ajax({
                    type: 'POST',
                    url: uri,
                    data: JSON.stringify(itemObj),
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (result) {
                        getCustomer();
                        $('#add-name').val('');
                        $('#add-telephone').val('');
                        $('#add-address').val('');
                        $('#add-email').val('');
                    }
                });
            }
    
            function deleteItem(rowid) {
                uri = 'https://localhost:44336/api/customer';
                $.ajax({
                    url: uri + '/' + rowid,
                    type: 'DELETE',
                    success: function (result) {
                        getCustomer()
                    }
                });
    
            }
    
            function editItem(rowid) {
                $.each(cus, function (key, item) {
                    if (item.customerId == rowid) {
                        $('#edit-id').val(item.customerId);
                        $('#edit-name').val(item.customerName);
                        $('#edit-telephone').val(item.customerTelephone);
                        $('#edit-address').val(item.customerAddress);
                        $('#edit-email').val(item.customerEmail);
                    }
                });
            }
    
            function editcustomer() {
                $("#spoilertwo").show();
                const item = {
                    'customerName': $('#edit-name').val(),
                    'customerTelephone': $('#edit-telephone').val(),
                    'customerAddress': $('#edit-address').val(),
                    'customerEmail': $('#edit-email').val()
    
                };
    
                $.ajax({
                    url: 'https://localhost:44336/api/customer/' + rowid,
                    type: 'PUT',
                    accepts: 'application/json',
                    contentType: 'application/json',
                    data: JSON.stringify(item),
                    success: function (result) {
                        getCustomer();
                        $('#edit-name').val('');
                        $('#edit-telephone').val('');
                        $('#edit-address').val('');
                        $('#edit-email').val('');
                    }
                });
    
                $("#spoiler").hide();
    
                return false;
            };
    
        </script>
    </body>
    </html>