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
ASP.NET Core Razor Pages - Pagination Example
ASP.NET Core Razor Pages - Pagination Example
@RenderBody()
@RenderSection("Scripts", required: false)
Index.cshtml
@page @model RazorPagesPagination.Pages.IndexModel
| @item.Id | @item.RealName | @item.UserName |
| No data! |
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
{
///
/// geovindu,Geovin Du,涂聚文
///
public class IndexModel : PageModel
{
public IEnumerable Items { get; set; }
public List DuItmes { get; set; }
public List 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
{
///
///
///
public int Id { get; set; }
///
///
///
public string RealName { get; set; }
///
///
///
public string UserName { get; set; }
public static List GetAllPerson()
{
List listPerson = new List
{
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 Pages { get; private set; }
}
}
@page
@model RazorPagesBook.IndexModel
@{
ViewData["Title"] = "Index";
}
Index
| @Html.DisplayNameFor(model => model.Book[0].Title) | @Html.DisplayNameFor(model => model.Book[0].PublicationDate) | @Html.DisplayNameFor(model => model.Book[0].Author) | @Html.DisplayNameFor(model => model.Book[0].Price) | @Html.DisplayNameFor(model => model.Book[0].Type) | |
|---|---|---|---|---|---|
| @Html.DisplayFor(modelItem => item.Title) | @Html.DisplayFor(modelItem => item.PublicationDate) | @Html.DisplayFor(modelItem => item.Author) | @Html.DisplayFor(modelItem => item.Price) | @Html.DisplayFor(modelItem => item.Type) | 编辑 | 详情 | 删除 |
| 没有查询到数据 |
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
{
///
/// geovindu,Geovin Du,涂聚文
///
public class IndexModel : PageModel
{
private readonly RazorPagesBook.Data.RazorPagesBookContext _context;
// public IEnumerable 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; }
///
///
///
///
public IndexModel(RazorPagesBook.Data.RazorPagesBookContext context)
{
_context = context;
}
///
/// 查询的关键字
/// geovindu, Geovin Du, 涂聚文
/// 塗聚文,天下为公
///
[BindProperty(SupportsGet = true)]
public string SearchKey { get; set; }
public IList 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();
// }
//}
///
///
///
///
///
//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();
}
}
///
///
///
///
///
///
///
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/");
}
}
}