Pager in Razor Pages
This article describes how to seamlessly integrate and configure the Telerik UI Pager for ASP.NET Core in Razor Pages applications.
Referencing Handler Methods in Razor Pages
Razor Pages is an alternative to the MVC pattern that makes page-focused coding easier and more productive. This approach consists of a cshtml file and a cshtml.cs file (by convention, the two files have the same name).
The cshtml.cs file, known as the PageModel, contains handler methods that respond to HTTP requests. These methods are prefixed with On followed by the HTTP verb (for example, OnGet, OnPost, OnPostRead, OnPostCreate).
Handler methods declared in a PageModel can be referenced from any Razor Page using one of the following URL patterns:
-
Using
Url.Page()C#Url.Page("PageName", "HandlerName") // OR Url.Page("/FolderName/PageName", "HandlerName")For example,
Url.Page("Index", "Read")references theOnPostReadorOnGetReadhandler method in theIndex.cshtml.csfile. -
Using a query string
C#Url("/PathToPage?handler=HandlerName")For example,
Url("/Index?handler=Read")references theOnPostReadorOnGetReadhandler method in theIndexpage.
For more information on Razor Pages architecture and concepts, refer to the official Microsoft documentation.
Binding to Remote Data
The Pager component provides a convenient integration with the Grid and DataSource. This example will demonstrate how to configure them in a Razor Pages scenario so that the Pager appear at the top of the Grid.
-
Define the Grid, DataSource, and Pager components. Specify the Read request URL of the DataSource. The URL must refer to the method name in the
PageModel.Razor@page @model IndexModel @using Kendo.Mvc.UI @(Html.Kendo().DataSource<OrderViewModel>() .Name("dataSource1") .Ajax(t => t .Read(r => r.Url(Url.Page("Index", "Read")).Data("forgeryToken")) .PageSize(20) ) ) @(Html.Kendo().Pager() .Name("pager") .DataSource("dataSource1") .ButtonCount(5) .PageSizes(true) ) @(Html.Kendo().Grid<OrderViewModel>() .Name("grid") .Columns(columns => { columns.Bound(p => p.OrderID).Filterable(false).Width(100); columns.Bound(p => p.Freight).Width(100); columns.Bound(p => p.OrderDate).Format("{0:MM/dd/yyyy}").Width(140); columns.Bound(p => p.ShipName); columns.Bound(p => p.ShipCity).Width(150); }) .Sortable() .Scrollable() .Filterable() .DataSource("dataSource1") ) -
Add an
AntiForgeryTokenat the top of the page.Razor@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() -
Send the
AntiForgeryTokenwith the Read request.JavaScript<script> function forgeryToken() { return kendo.antiForgeryTokens(); } </script>Additional parameters can also be supplied.
JavaScript<script> function forgeryToken() { return { __RequestVerificationToken: kendo.antiForgeryTokens().__RequestVerificationToken, additionalParameter: "test" } } </script> -
Within the
cshtml.csfile, add a handler method for the Read data operation.C#public class IndexModel : PageModel { public static IList<OrderViewModel> orders; public void OnGet() { if (orders == null) { // Populate the "orders" collection with data. orders = new List<OrderViewModel>(); Enumerable.Range(1, 50).ToList().ForEach(i => orders.Add(new OrderViewModel { OrderID = i, Freight = i * 10, ShipName = "ShipName " + i, ShipCity = "ShipCity " + i })); } } public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request) { return new JsonResult(orders.ToDataSourceResult(request)); } }