├── .gitignore
├── .vscode
├── launch.json
└── tasks.json
├── LICENSE
├── Pages
├── Index.cshtml
├── Index.cshtml.cs
├── Shared
│ └── _Layout.cshtml
├── _ViewImports.cshtml
└── _ViewStart.cshtml
├── Program.cs
├── README.md
├── RazorPagesPagination.csproj
├── Startup.cs
├── appsettings.Development.json
└── appsettings.json
/.gitignore:
--------------------------------------------------------------------------------
1 | # .NET compiled files
2 | bin
3 | obj
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to find out which attributes exist for C# debugging
3 | // Use hover for the description of the existing attributes
4 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "name": ".NET Core Launch (web)",
9 | "type": "coreclr",
10 | "request": "launch",
11 | "preLaunchTask": "build",
12 | // If you have changed target frameworks, make sure to update the program path.
13 | "program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/RazorPagesPagination.dll",
14 | "args": [],
15 | "cwd": "${workspaceFolder}",
16 | "stopAtEntry": false,
17 | "internalConsoleOptions": "openOnSessionStart",
18 | "launchBrowser": {
19 | "enabled": true,
20 | "args": "${auto-detect-url}",
21 | "windows": {
22 | "command": "cmd.exe",
23 | "args": "/C start ${auto-detect-url}"
24 | },
25 | "osx": {
26 | "command": "open"
27 | },
28 | "linux": {
29 | "command": "xdg-open"
30 | }
31 | },
32 | "env": {
33 | "ASPNETCORE_ENVIRONMENT": "Development"
34 | },
35 | "sourceFileMap": {
36 | "/Views": "${workspaceFolder}/Views"
37 | }
38 | },
39 | {
40 | "name": ".NET Core Attach",
41 | "type": "coreclr",
42 | "request": "attach",
43 | "processId": "${command:pickProcess}"
44 | }
45 | ,]
46 | }
--------------------------------------------------------------------------------
/.vscode/tasks.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "2.0.0",
3 | "tasks": [
4 | {
5 | "label": "build",
6 | "command": "dotnet",
7 | "type": "process",
8 | "args": [
9 | "build",
10 | "${workspaceFolder}/RazorPagesPagination.csproj"
11 | ],
12 | "problemMatcher": "$msCompile"
13 | }
14 | ]
15 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2018 Jason Watmore
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Pages/Index.cshtml:
--------------------------------------------------------------------------------
1 | @page
2 | @model RazorPagesPagination.Pages.IndexModel
3 |
4 |
5 |
21 |
22 |
23 |
24 | @foreach (var item in Model.Items)
25 | {
26 |
27 | @item |
28 |
29 | }
30 |
31 |
32 |
33 | @if (Model.Pager.Pages.Any())
34 | {
35 |
65 | }
--------------------------------------------------------------------------------
/Pages/Index.cshtml.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Microsoft.AspNetCore.Mvc.RazorPages;
4 | using Microsoft.AspNetCore.Mvc.Rendering;
5 | using Microsoft.AspNetCore.Http;
6 | using Microsoft.AspNetCore.Mvc;
7 | using JW;
8 |
9 | namespace RazorPagesPagination.Pages
10 | {
11 | public class IndexModel : PageModel
12 | {
13 | public IEnumerable Items { get; set; }
14 | public Pager Pager { get; set; }
15 | public SelectList TotalItemsList { get; set; }
16 | public int TotalItems { get; set; }
17 | public SelectList PageSizeList { get; set; }
18 | public int PageSize { get; set; }
19 | public SelectList MaxPagesList { get; set; }
20 | public int MaxPages { get; set; }
21 |
22 | public void OnGet(int p = 1)
23 | {
24 | // properties for pager parameter controls
25 | TotalItemsList = new SelectList(new []{ 10, 150, 500, 1000, 5000, 10000, 50000, 100000, 1000000 });
26 | TotalItems = HttpContext.Session.GetInt32("TotalItems") ?? 150;
27 | PageSizeList = new SelectList(new []{ 1, 5, 10, 20, 50, 100, 200, 500, 1000 });
28 | PageSize = HttpContext.Session.GetInt32("PageSize") ?? 10;
29 | MaxPagesList = new SelectList(new []{ 1, 5, 10, 20, 50, 100, 200, 500 });
30 | MaxPages = HttpContext.Session.GetInt32("MaxPages") ?? 10;
31 |
32 | // generate list of sample items to be paged
33 | var dummyItems = Enumerable.Range(1, TotalItems).Select(x => "Item " + x);
34 |
35 | // get pagination info for the current page
36 | Pager = new Pager(dummyItems.Count(), p, PageSize, MaxPages);
37 |
38 | // assign the current page of items to the Items property
39 | Items = dummyItems.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize);
40 | }
41 |
42 | public IActionResult OnPost(int totalItems, int pageSize, int maxPages)
43 | {
44 | // update pager parameters for session and redirect back to 'OnGet'
45 | HttpContext.Session.SetInt32("TotalItems", totalItems);
46 | HttpContext.Session.SetInt32("PageSize", pageSize);
47 | HttpContext.Session.SetInt32("MaxPages", maxPages);
48 | return Redirect("/");
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/Pages/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | ASP.NET Core Razor Pages - Pagination Example
7 |
8 |
9 |
10 |
11 |
12 |
ASP.NET Core Razor Pages - Pagination Example
13 | @RenderBody()
14 |
15 |
16 |
17 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Pages/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @using RazorPagesPagination
2 | @namespace RazorPagesPagination.Pages
3 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4 |
--------------------------------------------------------------------------------
/Pages/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
4 |
--------------------------------------------------------------------------------
/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore;
2 | using Microsoft.AspNetCore.Hosting;
3 |
4 | namespace RazorPagesPagination
5 | {
6 | public class Program
7 | {
8 | public static void Main(string[] args)
9 | {
10 | CreateWebHostBuilder(args).Build().Run();
11 | }
12 |
13 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
14 | WebHost.CreateDefaultBuilder(args)
15 | .UseStartup();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # aspnet-core-razor-pages-pagination
2 |
3 | ASP.NET Core Razor Pages - Pagination Example
4 |
5 | For documentation and instructions check out http://jasonwatmore.com/post/2018/10/15/aspnet-core-razor-pages-pagination-example
--------------------------------------------------------------------------------
/RazorPagesPagination.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.1
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Startup.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Builder;
6 | using Microsoft.AspNetCore.Hosting;
7 | using Microsoft.AspNetCore.Http;
8 | using Microsoft.AspNetCore.HttpsPolicy;
9 | using Microsoft.AspNetCore.Mvc;
10 | using Microsoft.Extensions.Configuration;
11 | using Microsoft.Extensions.DependencyInjection;
12 |
13 | namespace RazorPagesPagination
14 | {
15 | public class Startup
16 | {
17 | public Startup(IConfiguration configuration)
18 | {
19 | Configuration = configuration;
20 | }
21 |
22 | public IConfiguration Configuration { get; }
23 |
24 | // This method gets called by the runtime. Use this method to add services to the container.
25 | public void ConfigureServices(IServiceCollection services)
26 | {
27 | services.AddSession();
28 | services.AddMvc();
29 | }
30 |
31 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
32 | public void Configure(IApplicationBuilder app, IHostingEnvironment env)
33 | {
34 | app.UseSession();
35 | app.UseMvc();
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Warning"
5 | }
6 | },
7 | "AllowedHosts": "*"
8 | }
9 |
--------------------------------------------------------------------------------