如何在 ASP.NET 核心 MVC 中执行分页

How to perform Pagination in ASP.NET Core MVC

我正在尝试对 ASP.NET Core MVC 和 Entity Framework 中的 table 记录进行分页。我关注了 this video,这是我的代码:

Pager.cs

using System;

namespace WebApp.Models
{
    public class Pager
    {
        public int TotalItems { get; private set; }
        public int CurrentPage { get; private set; }
        public int PageSize { get; private set; }
        public int TotalPages { get; private set; }
        public int StartPage { get; private set; }
        public int EndPage { get; private set; }

        public Pager()
        {    
        }

        public Pager(int totalItems, int page, int pageSize = 10)
        {
            int totalPages = (int)Math.Ceiling((decimal)totalItems / (decimal)pageSize);
            int currentPage = page;
            int startPage = currentPage - 5;
            int endPage = currentPage + 4;

            if (startPage <= 0)
            {
                endPage = endPage - (startPage - 1);
                startPage = 1;
            }

            if (endPage > totalPages)
            {
                endPage = totalPages;

                if (endPage > 10)
                {
                    startPage = endPage - 9;
                }
            }

            TotalItems = totalItems;
            CurrentPage = currentPage;
            PageSize = pageSize;
            TotalPages = totalPages;
            StartPage = startPage;
            EndPage = endPage;
        }
    }
}

旅游控制器:

public IActionResult AllTours(int pg = 1)
{
        List<Tour> tours = _context.Tour.ToList();
        const int pageSize = 9;

        if (pg < 1)
            pg = 1;

        int recsCount = tours.Count();
        var pager = new Pager(recsCount,pg,pageSize);
        int recSkip = (pg - 1)* pageSize;
        var data = tours.Skip(recSkip).Take(pager.PageSize).ToList();
        this.ViewBag.Pager= pager;

        return View(data);
}

Index.cshtml:

@model IEnumerable<WebApp.Models.Tour>
@{
    ViewData["Title"] = "Tours";
    Pager pager=new Pager();
    int pageNo = 0;

    if (ViewBag.Pager != null)
    {
        pager = ViewBag.Pager;
        pageNo = pager.CurrentPage;
    }
}
<div class="row">
            @if(pager.TotalPages > 0)
                {
                    <div class="col-lg-12">
                        
                            <div class="pagination mt-30">
                                <ul class="pagination mt-30">
                                    @for(var pge = pager.StartPage;pge <= pager.EndPage;pge++)
                                    {
                                        <li class="@(pge == pager.CurrentPage ? "active" : "")">
                                            <a asp-controller="Tour" asp-action="AllTours" asp-route-pg="@pge">@pge</a>
                                        </li>
                                    }   
                                </ul>    
                            </div>    
                    </div>
                }
        </div>

但是这段代码没有做分页,当我点击页码时,我得到这个错误:

This localhost page can’t be found No webpage was found for the web address: https://localhost:5001/Tour/AllTours?pg=2 HTTP ERROR 404

检查错误信息,它显示:

https://localhost:5001/Tour/AllTours?pg=2

但是在你的问题中,你提供的控制器是ToursController,所以尝试将asp-controller="Tour"更改为asp-controller="Tours"

我很疑惑的一点是你的action名字是AllTours,一般对应的页面是AllTours.cshtml,但是你的问题里写的是Index.cshtml,所以我我不确定你是否把问题写错了,或者你是否真的在 Index.cshtml.

上写了页面

默认路由模板 {controller=Home}/{action=Index}/{id?},因此如果您将操作 AllTours 写入页面 Index.cshtml,url 将无法正确导航到该页面。