Pager 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 design, the two files have the same name).
You can seamlessly integrate the Telerik UI Pager for ASP.NET MVC in Razor Pages applications.
This article describes how to configure the Pager component in a Razor Pages scenario.
Getting Started
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.
-
Create the Grid, DataSource and Pager definitions.
HtmlHelper_Index.cshtml@page @model IndexModel @using Kendo.Mvc.UI @(Html.Kendo().DataSource<OrderViewModel>() .Name("dataSource1") .Ajax(t => t.Read(r => r.Url("/Index?handler=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
AntiForgeryToken
at the top of the page.Razor@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken()
-
Send the
AntiForgeryToken
with the Read request.Razor<script> function forgeryToken() { return kendo.antiForgeryTokens(); } </script>
Additional parameters can also be supplied.
Razor<script> function forgeryToken() { return { __RequestVerificationToken: kendo.antiForgeryTokens().__RequestVerificationToken, additionalParameter: "test" } } </script>
-
Within the
cshtml.cs
file, add a handler method for the Read data operation.tab-Index.cshtml.cspublic 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)); }