New to Telerik UI for ASP.NET CoreStart a free 30-day trial

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 Core 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.

  1. 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")
            )
  2. Add an AntiForgeryToken at the top of the page.

    Razor
        @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
        @Html.AntiForgeryToken()
  3. 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>
  4. Within the cshtml.cs file, add a handler method for the Read data operation.

    Index.cshtml.cs
        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));
        }

See Also

In this article
Getting StartedSee Also
Not finding the help you need?
Contact Support