• Csharp: ASP.NET Core 3.1 Razor Pages Query and Pagination


    Microsoft.EntityFrameworkCore.SqlServer SqlServer 2012及以上 https://github.com/dotnet/efcore
    Microsoft.EntityFrameworkCore.Sqlite Sqlite 3.7及以上 https://github.com/dotnet/efcore
    Microsoft.EntityFrameworkCore.InMemory EF Core内存中的数据库 https://github.com/dotnet/efcore
    Microsoft.EntityFrameworkCore.Cosmos Azure Cosmos DB SQL API https://github.com/dotnet/efcore
    Npgsql.EntityFrameworkCore.PostgreSQL PostgreSQL https://github.com/npgsql/efcore.pg
    Pomelo.EntityFrameworkCore.MySql MySql,MariaDB https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql
    Pomelo.EntityFrameworkCore.MyCat MyCat服务器 https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MyCat
    EntityFrameworkCore.SqlServerCompact40 https://github.com/ErikEJ/EntityFramework.SqlServerCompact
    EntityFrameworkCore.SqlServerCompact35 https://github.com/ErikEJ/EntityFramework.SqlServerCompact
    FirebirdSql.EntityFrameworkCore.Firebird FirebirdSQL 2.5及3.X https://github.com/FirebirdSQL/NETProvider
    EntityFrameworkCore.FirebirdSQL FirebirdSQL 2.5及3.X https://github.com/ralmsdeveloper/EntityFrameworkCore.FirebirdSQL
    MySql.Data.EntityFrameworkCore MySql
    Oracle.EntityFrameworkCore Oracle DB 11.2 及更高版本
    IBM.EntityFrameworkCore DB2,Informix
    IBM.EntityFrameworkCore-lnx DB2,Informix
    IBM.EntityFrameworkCore-osx DB2,Informix
    EntityFrameworkCore.Jet Microsoft Access https://github.com/bubibubi/EntityFrameworkCore.Jet
    EntityFrameworkCore.OpenEdge Progress OpenEdge https://github.com/alexwiese/EntityFrameworkCore.OpenEdge
    Devart.Data.Oracle.EFCore Oracle DB 9.0及更高版本
    Devart.Data.PostgreSql.EFCore PostgreSql 8.0及以上版本
    Devart.Data.SQLite.EFCore SQLite 3及以上版本
    Devart.Data.MySql.EFCore MySql 5及以上版本
    FileContextCore 在文件中存储数据 https://github.com/pmizel/DevMentor.Context.FileContext

    libman.json:

    {
      "version": "1.0", //当前的libman文件版本
      "defaultProvider": "cdnjs", //默认从哪个CDN网络下载文件
      "libraries": [
        {
          "library": "twitter-bootstrap@4.3.1", //要下载的前端包名称
          "destination": "wwwroot/lib/twitter-bootstrap/" //存放库的文件路径地址
        },
        {
          "library": "jquery@3.4.1", //要下载的前端包名称
          "destination": "wwwroot/lib/jquery/", //存放库的文件路径地址
          "provider": "jsdelivr", //针对某个独立的文件,从其他源下载。
          "files": [ "dist/jquery.js", "dist/jquery.min.js" ] //下载该库中特定的文件,而不是下载所有的文件
    
        },
        {
          "library": "jquery-validate@1.19.1",
          "destination": "wwwroot/lib/jquery-validate"
        },
        {
          "library": "jquery-validation-unobtrusive@3.2.11",
          "destination": "wwwroot/lib/jquery-validate-unobtrusive"
        },
        {
          "provider": "jsdelivr",
          "library": "font-awesome@4.7.0",
          "destination": "wwwroot/lib/font-awesome"
        }
      ]
    }
    

      

    Startup.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    
    namespace RazorPagesPagination
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddSession();
                services.AddMvc();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                //wwwroot 下的文件夹配置
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
    
                app.UseHttpsRedirection();
                app.UseStaticFiles();
                app.UseSession();
                app.UseMvc();
            }
        }
    }
    

      

    _Layout.cshtml

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>ASP.NET Core Razor Pages - Pagination Example</title>
        <meta name="description" content=" geovindu,Geovin Du,涂聚文" />
        <meta name="keywords" content="geovindu,Geovin Du" />
        <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
    
        <link href="~/lib/twitter-bootstrap/css/bootstrap.css" rel="stylesheet" />
    
    
    </head>
    <body>
        <div class="container text-center">
            <div class="col">
                <h1>ASP.NET Core Razor Pages - Pagination Example</h1>
                @RenderBody()
            </div>
        </div>
        <hr />
        <div class="credits text-center">
            <p>
                <a href="http://jasonwatmore.com/post/2018/10/15/aspnet-core-razor-pages-pagination-example">ASP.NET Core Razor Pages - Pagination Example</a>
            </p>
            <p>
                <a href="http://jasonwatmore.com">JasonWatmore.com</a>
            </p>
        </div>
    
    
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/lib/twitter-bootstrap/js/bootstrap.min.js"></script>
    
        @RenderSection("Scripts", required: false)
    </body>
    </html>
    

      

    Index.cshtml

    @page
    @model RazorPagesPagination.Pages.IndexModel
    <!-- pager parameter controls -->
    <form method="post" class="container border text-left pt-2 mb-3">
        <div class="form-row form-group">
            <div class="col">
                <label asp-for="TotalItems">Total number of items</label>
                <select asp-for="TotalItems" asp-items="Model.TotalItemsList" class="form-control form-control-sm" onchange="this.form.submit()"></select>
            </div>
            <div class="col">
                <label asp-for="PageSize">Items per page</label>
                <select asp-for="PageSize" asp-items="Model.PageSizeList" class="form-control form-control-sm" onchange="this.form.submit()"></select>
            </div>
            <div class="col">
                <label asp-for="MaxPages">Max page links displayed</label>
                <select asp-for="MaxPages" asp-items="Model.MaxPagesList" class="form-control form-control-sm" onchange="this.form.submit()"></select>
            </div>
    
            标题: <input type="text" asp-for="SearchKey" value="@Model.SearchKey" onchange="this.form.submit()" />
            <input type="submit" value="查询" onchange="this.form.submit()" />
        </div>
    </form>
    
    <!-- items being paged -->
    <table class="table table-sm table-striped table-bordered">
        @if (!Object.Equals(Model.DuItmes, null))
        {
            @foreach (var item in Model.DuItmes)
            {
                <tr>
                    <td>@item.Id</td>
                    <td>@item.RealName</td>
                    <td>@item.UserName</td>
                </tr>
            }
        }
        else
        {
            <tr>
                <td>No data!</td>
            </tr>
    
        }
    </table>
    
    <!-- pager -->
    @if (!Object.Equals(Model.DuItmes, null))
    {
        @if (Model.Pager.Pages.Any())
        {
            <nav class="table-responsive">
                <ul class="pagination justify-content-center d-flex flex-wrap">
                    @if (Model.Pager.CurrentPage > 1)
                    {
                        <li class="page-item">
                            <a class="page-link" href="/">First</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="/?p=@(Model.Pager.CurrentPage - 1)">Previous</a>
                        </li>
                    }
    
                    @foreach (var p in Model.Pager.Pages)
                    {
                        <li class="page-item @(p == Model.Pager.CurrentPage ? "active" : "")">
                            <a class="page-link" href="/?p=@p">@p</a>
                        </li>
                    }
    
                    @if (Model.Pager.CurrentPage < Model.Pager.TotalPages)
                    {
                        <li class="page-item">
                            <a class="page-link" href="/?p=@(Model.Pager.CurrentPage + 1)">Next</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="/?p=@(Model.Pager.TotalPages)">Last</a>
                        </li>
                    }
                </ul>
            </nav>
        }
    }
    

      

    Index.cshtml.cs

    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using JW; //JW.Pager
    
    //https://github.com/cornflourblue/JW.Pager
    //
    
    namespace RazorPagesPagination.Pages
    {
    
        /// <summary>
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class IndexModel : PageModel
        {
            public IEnumerable<string> Items { get; set; }
    
            public List<Person> DuItmes { get; set; }
    
            public List<Person> dummyItems { get; set; }
            public Pager Pager { get; set; }
            public SelectList TotalItemsList { get; set; }
            public int TotalItems { get; set; }
            public SelectList PageSizeList { get; set; }
            public int PageSize { get; set; }
            public SelectList MaxPagesList { get; set; }
            public int MaxPages { get; set; }
    
            [BindProperty(SupportsGet = true)]
            public string SearchKey { get; set; }
    
            public void OnGet(int p = 1)
            {
                // properties for pager parameter controls
                TotalItemsList = new SelectList(new []{ 10, 150, 500, 1000, 5000, 10000, 50000, 100000, 1000000 });
                TotalItems = HttpContext.Session.GetInt32("TotalItems") ?? 150;
                PageSizeList = new SelectList(new []{ 1, 5, 10, 20, 50, 100, 200, 500, 1000 });
                PageSize = HttpContext.Session.GetInt32("PageSize") ?? 10;
                MaxPagesList = new SelectList(new []{ 1, 5, 10, 20, 50, 100, 200, 500 });
                MaxPages = HttpContext.Session.GetInt32("MaxPages") ?? 10;
                SearchKey = HttpContext.Session.GetString("SearchKey" ?? "");
                // generate list of sample items to be paged
    
                //查询一遍是空值
                var dummyItems = Person.GetAllPerson().AsQueryable();// var dummyItems =Enumerable.Range(1, TotalItems).Select(x => "Item " + x);
                if(object.Equals(dummyItems,null))
                {
                   var dummyItemdd = Person.GetAllPerson();
                    dummyItems = dummyItemdd.AsQueryable();
                }
                if (!string.IsNullOrEmpty(SearchKey))
                {
                    dummyItems = dummyItems.Where(b => b.RealName.Contains(SearchKey)|| b.UserName.Contains(SearchKey));
                    if (dummyItems.Count() > 0)
                    {
                        Pager = new Pager(dummyItems.Count(), p, PageSize, MaxPages);
    
                        // assign the current page of items to the Items property  DuItmes
                        //Items = dummyItems.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize);
                        DuItmes = dummyItems.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize).ToList();
                    }
                }
                else
                {
                    // get pagination info for the current page
                    Pager = new Pager(dummyItems.Count(), p, PageSize, MaxPages);
    
                    // assign the current page of items to the Items property
                    //Items = dummyItems.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize);
                    DuItmes = dummyItems.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize).ToList();
                }
            }
    
            public IActionResult OnPost(int totalItems, int pageSize, int maxPages)
            {
                // update pager parameters for session and redirect back to 'OnGet'
                HttpContext.Session.SetInt32("TotalItems", totalItems);
                HttpContext.Session.SetInt32("PageSize", pageSize);
                HttpContext.Session.SetInt32("MaxPages", maxPages);
                HttpContext.Session.SetString("SearchKey", SearchKey);
                return Redirect("/");
            }
        }
    
    
        public class Person
        {
            /// <summary>
            /// 
            /// </summary>
            public int Id { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string RealName { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string UserName { get; set; }
    
    
            public static List<Person> GetAllPerson()
            {
                List<Person> listPerson = new List<Person>
                {
                    new Person{Id=1,RealName="涂聚文",UserName="geovindu1" },
                    new Person{Id=2,RealName="涂聚文",UserName="geovindu2" },
                    new Person{Id=3,RealName="涂聚文",UserName="geovindu3" },
                    new Person{Id=4,RealName="涂聚文",UserName="geovindu4" },
                    new Person{Id=5,RealName="涂聚文",UserName="geovindu5" },
                    new Person{Id=6,RealName="涂聚文",UserName="geovindu6" },
                    new Person{Id=7,RealName="涂聚文",UserName="geovindu7" },
                    new Person{Id=8,RealName="涂聚文",UserName="geovindu8" },
                    new Person{Id=9,RealName="涂聚文",UserName="geovindu9" },
                    new Person{Id=10,RealName="涂聚文",UserName="geovindu10" },
                    new Person{Id=11,RealName="涂聚文",UserName="geovindu11" },
                    new Person{Id=12,RealName="涂聚文",UserName="geovindu12" },
                    new Person{Id=13,RealName="涂聚文",UserName="geovindu13" },
                    new Person{Id=14,RealName="涂聚文",UserName="geovindu14" },
                    new Person{Id=15,RealName="涂聚文",UserName="geovindu15" },
                    new Person{Id=16,RealName="涂聚文",UserName="geovindu16" },
                    new Person{Id=17,RealName="涂聚文",UserName="geovindu17" },
                    new Person{Id=18,RealName="涂聚文",UserName="geovindu18" },
                    new Person{Id=19,RealName="涂聚文",UserName="geovindu19" },
                    new Person{Id=20,RealName="涂聚文",UserName="geovindu20" },
                    new Person{Id=21,RealName="涂聚文",UserName="geovindu21" },
                    new Person{Id=22,RealName="涂聚文",UserName="geovindu22" },
                    new Person{Id=23,RealName="涂聚文",UserName="geovindu23" },
                    new Person{Id=24,RealName="涂聚文",UserName="geovindu24" },
                    new Person{Id=25,RealName="涂聚文",UserName="geovindu25" },
                    new Person{Id=26,RealName="涂聚文",UserName="geovindu26" },
                    new Person{Id=27,RealName="涂聚文",UserName="geovindu27" },
                    new Person{Id=28,RealName="涂聚文",UserName="geovindu28" },
                    new Person{Id=29,RealName="涂聚文",UserName="geovindu29" },
                    new Person{Id=30,RealName="涂聚文",UserName="geovindu30" },
                };
    
                return listPerson;
            }
        }
    }
    

      

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace JW
    {
        //JW.Pager 
        //https://github.com/cornflourblue/JW.Pager/blob/master/Pager.cs
        public class Pager
        {
            public Pager(
                int totalItems,
                int currentPage = 1,
                int pageSize = 10,
                int maxPages = 10)
            {
                // calculate total pages
                var totalPages = (int)Math.Ceiling((decimal)totalItems / (decimal)pageSize);
    
                // ensure current page isn't out of range
                if (currentPage < 1)
                {
                    currentPage = 1;
                }
                else if (currentPage > totalPages)
                {
                    currentPage = totalPages;
                }
    
                int startPage, endPage;
                if (totalPages <= maxPages) 
                {
                    // total pages less than max so show all pages
                    startPage = 1;
                    endPage = totalPages;
                }
                else 
                {
                    // total pages more than max so calculate start and end pages
                    var maxPagesBeforeCurrentPage = (int)Math.Floor((decimal)maxPages / (decimal)2);
                    var maxPagesAfterCurrentPage = (int)Math.Ceiling((decimal)maxPages / (decimal)2) - 1;
                    if (currentPage <= maxPagesBeforeCurrentPage) 
                    {
                        // current page near the start
                        startPage = 1;
                        endPage = maxPages;
                    } 
                    else if (currentPage + maxPagesAfterCurrentPage >= totalPages) 
                    {
                        // current page near the end
                        startPage = totalPages - maxPages + 1;
                        endPage = totalPages;
                    }
                    else 
                    {
                        // current page somewhere in the middle
                        startPage = currentPage - maxPagesBeforeCurrentPage;
                        endPage = currentPage + maxPagesAfterCurrentPage;
                    }
                }
    
                // calculate start and end item indexes
                var startIndex = (currentPage - 1) * pageSize;
                var endIndex = Math.Min(startIndex + pageSize - 1, totalItems - 1);
    
                // create an array of pages that can be looped over
                var pages = Enumerable.Range(startPage, (endPage + 1) - startPage);
    
                // update object instance with all pager properties required by the view
                TotalItems = totalItems;
                CurrentPage = currentPage;
                PageSize = pageSize;
                TotalPages = totalPages;
                StartPage = startPage;
                EndPage = endPage;
                StartIndex = startIndex;
                EndIndex = endIndex;
                Pages = pages;
            }
    
            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 int StartIndex { get; private set; }
            public int EndIndex { get; private set; }
            public IEnumerable<int> Pages { get; private set; }
        }
    }
    

      

    @page
    @model RazorPagesBook.IndexModel
    
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <p>
        <a asp-page="Create">Create New</a>
    </p>
    <form method="post" class="container border text-left pt-2 mb-3">
        <div class="form-row form-group">
            <div class="col">
                <label asp-for="TotalItems">Total number of items</label>
                <select asp-for="TotalItems" asp-items="Model.TotalItemsList" class="form-control form-control-sm" onchange="this.form.submit()"></select>
            </div>
            <div class="col">
                <label asp-for="PageSize">Items per page</label>
                <select asp-for="PageSize" asp-items="Model.PageSizeList" class="form-control form-control-sm" onchange="this.form.submit()"></select>
            </div>
            <div class="col">
                <label asp-for="MaxPages">Max page links displayed</label>
                <select asp-for="MaxPages" asp-items="Model.MaxPagesList" class="form-control form-control-sm" onchange="this.form.submit()"></select>
            </div>
        </div>
        <p>
            标题: <input type="text" asp-for="SearchKey" value="@Model.SearchKey" />
            <input type="submit" value="查询" />
        </p>
    </form>
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Book[0].Title)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Book[0].PublicationDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Book[0].Author)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Book[0].Price)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Book[0].Type)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @if (Object.Equals(Model.Book, null))
            {
                @foreach (var item in Model.Book)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.Title)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.PublicationDate)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Author)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Price)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Type)
                        </td>
                        <td>
                            <a asp-page="./Edit" asp-route-id="@item.ID">编辑</a> |
                            <a asp-page="./Details" asp-route-id="@item.ID">详情</a> |
                            <a asp-page="./Delete" asp-route-id="@item.ID">删除</a>
                        </td>
                    </tr>
                }
            }
            else
            {
                <tr>
                    <td>没有查询到数据</td>
                </tr>
    
             }
            </tbody>
    </table>
    <!-- pager -->
    @if (Object.Equals(Model.Book, null))
    {
        @if (Model.Pager.Pages.Any())
        {
            <nav class="table-responsive">
                <ul class="pagination justify-content-center d-flex flex-wrap">
                    @if (Model.Pager.CurrentPage > 1)
                    {
                        <li class="page-item">
                            <a class="page-link" href="/Books/">First</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="/Books/?p=@(Model.Pager.CurrentPage - 1)">Previous</a>
                        </li>
                    }
    
                    @foreach (var p in Model.Pager.Pages)
                    {
                        <li class="page-item @(p == Model.Pager.CurrentPage ? "active" : "")">
                            <a class="page-link" href="/Books/?p=@p">@p</a>
                        </li>
                    }
    
                    @if (Model.Pager.CurrentPage < Model.Pager.TotalPages)
                    {
                        <li class="page-item">
                            <a class="page-link" href="/Books/?p=@(Model.Pager.CurrentPage + 1)">Next</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="/Books/?p=@(Model.Pager.TotalPages)">Last</a>
                        </li>
                    }
                </ul>
            </nav>
        }
    }
    

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.EntityFrameworkCore;
    using RazorPagesBook.Data;
    using RazorPagesBook.Models;
    
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Http;
    
    
    
    
    namespace RazorPagesBook
    {
    
    
        /// <summary>
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class IndexModel : PageModel
        {
            private readonly RazorPagesBook.Data.RazorPagesBookContext _context;
    
           // public IEnumerable<string> Items { get; set; }
            public DuPager Pager { get; set; }
            public SelectList TotalItemsList { get; set; }
            public int TotalItems { get; set; }
            public SelectList PageSizeList { get; set; }
            public int PageSize { get; set; }
            public SelectList MaxPagesList { get; set; }
            public int MaxPages { get; set; }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="context"></param>
            public IndexModel(RazorPagesBook.Data.RazorPagesBookContext context)
            {
                _context = context;
            }
            /// <summary>
            /// 查询的关键字
            /// geovindu, Geovin Du, 涂聚文
            /// 塗聚文,天下为公
            /// </summary>
            [BindProperty(SupportsGet = true)]
            public string SearchKey { get; set; }
    
            public IList<Book> Book { get;set; }
    
            //public async Task OnGetAsync()
            //{
            //    TotalItemsList = new SelectList(new[] { 10, 150, 500, 1000, 5000, 10000, 50000, 100000, 1000000 });
            //    TotalItems = HttpContext.Session.GetInt32("TotalItems") ?? 150;
            //    PageSizeList = new SelectList(new[] { 1, 5, 10, 20, 50, 100, 200, 500, 1000 });
            //    PageSize = HttpContext.Session.GetInt32("PageSize") ?? 10;
            //    MaxPagesList = new SelectList(new[] { 1, 5, 10, 20, 50, 100, 200, 500 });
            //    MaxPages = HttpContext.Session.GetInt32("MaxPages") ?? 10;
    
    
            //    var books = _context.Book.AsQueryable();
            //    //var books = from b in _context.Book select b;
            //    if (!string.IsNullOrEmpty(SearchKey))
            //    {
            //        books = books.Where(b => b.Title.Contains(SearchKey));
            //        Pager = new DuPager(books.Count(), Pager.CurrentPage, PageSize, MaxPages);
            //        Book = await books.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize).ToListAsync();
    
    
            //    }
            //    else
            //    {
            //        //var page = parents.SelectMany(p => p.Children).Skip(PageIndex * PageSize).Take(PageSize);
            //        Pager = new DuPager(books.Count(), Pager.CurrentPage, PageSize, MaxPages);
            //        Book = await books.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize).ToListAsync();
            //        // Book = await books.ToListAsync();
            //    }
            //}
            /// <summary>
            /// 
            /// </summary>
            /// <param name="p"></param>
            /// <returns></returns>
            //public async Task OnGetAsync(int p = 1)
            //{
            //    TotalItemsList = new SelectList(new[] { 10, 150, 500, 1000, 5000, 10000, 50000, 100000, 1000000 });
            //    TotalItems = HttpContext.Session.GetInt32("TotalItems") ?? 150;
            //    PageSizeList = new SelectList(new[] { 1, 5, 10, 20, 50, 100, 200, 500, 1000 });
            //    PageSize = HttpContext.Session.GetInt32("PageSize") ?? 10;
            //    MaxPagesList = new SelectList(new[] { 1, 5, 10, 20, 50, 100, 200, 500 });
            //    MaxPages = HttpContext.Session.GetInt32("MaxPages") ?? 10;
    
    
            //    var books = _context.Book.AsQueryable();
            //    //var books = from b in _context.Book select b;
            //    if (!string.IsNullOrEmpty(SearchKey))
            //    {
            //        books = books.Where(b => b.Title.Contains(SearchKey));
            //        Pager = new DuPager(books.Count(), p, PageSize, MaxPages);
            //        Book = await books.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize).ToListAsync();
            //    }
            //    else
            //    {
            //        //var page = parents.SelectMany(p => p.Children).Skip(PageIndex * PageSize).Take(PageSize);
            //        Pager = new DuPager(books.Count(), p, PageSize, MaxPages);
            //        // Book = await books.ToListAsync();
            //        Book = await books.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize).ToListAsync();
            //    }
    
            //}
    
    
            public void OnGet(int p = 1)
            {
                // properties for pager parameter controls
                TotalItemsList = new SelectList(new[] { 10, 150, 500, 1000, 5000, 10000, 50000, 100000, 1000000 });
                TotalItems = HttpContext.Session.GetInt32("TotalItems") ?? 150;
                PageSizeList = new SelectList(new[] { 1, 5, 10, 20, 50, 100, 200, 500, 1000 });
                PageSize = HttpContext.Session.GetInt32("PageSize") ?? 10;
                MaxPagesList = new SelectList(new[] { 1, 5, 10, 20, 50, 100, 200, 500 });
                MaxPages = HttpContext.Session.GetInt32("MaxPages") ?? 10;
                SearchKey=HttpContext.Session.GetString("SearchKey"??"");
                // generate list of sample items to be paged
                var books = _context.Book.AsQueryable(); //Enumerable.Range(1, TotalItems).Select(x => "Item " + x);
    
                // get pagination info for the current page
                if (!string.IsNullOrEmpty(SearchKey))
                {
                    books = books.Where(b => b.Title.Contains(SearchKey)||b.Author.Contains(SearchKey));
                    if (books.Count() > 0)
                    {
                        Pager = new DuPager(books.Count(), p, PageSize, MaxPages);
                        Book = books.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize).ToList();
                    }
                }
                else
                {
    
                    Pager = new DuPager(books.Count(), p, PageSize, MaxPages);
                    // assign the current page of items to the Items property
                    // Book = books.SelectMany(p=>p.Title).Skip(Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize);
                    // var results = books.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize);
    
                    Book = books.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize).ToList();// books.ToList();
                }
                    
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="totalItems"></param>
            /// <param name="pageSize"></param>
            /// <param name="maxPages"></param>
            /// <returns></returns>
            public IActionResult OnPost(int totalItems, int pageSize, int maxPages)
            {
                // update pager parameters for session and redirect back to 'OnGet'
                HttpContext.Session.SetInt32("TotalItems", totalItems);
                HttpContext.Session.SetInt32("PageSize", pageSize);
                HttpContext.Session.SetInt32("MaxPages", maxPages);
                HttpContext.Session.SetString("SearchKey", SearchKey);
                //查询返回页面
                return Redirect("/Books/");
            }
        }
    }
    

      

     

  • 相关阅读:
    Linux下多线程查看工具(pstree、ps、pstack)
    linux的netstat命令详解
    linux的netstat命令详解
    实例解说Linux命令行uniq
    实例解说Linux命令行uniq
    实例解说Linux命令行uniq
    linux之sort用法
    linux之sort用法
    linux之sort用法
    oracle服务器和客户端字符集的查看和修改
  • 原文地址:https://www.cnblogs.com/geovindu/p/16169310.html
Copyright © 2020-2023  润新知