# 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 the `OnPostRead` or `OnGetRead` handler method in the `Index.cshtml.cs` file.

* Using a query string

  ```C#
  Url("/PathToPage?handler=HandlerName")
  ```

  For example, `Url("/Index?handler=Read")` references the `OnPostRead` or `OnGetRead` handler method in the `Index` page.

For more information on Razor Pages architecture and concepts, refer to [the official Microsoft documentation](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/).

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

1. 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 HtmlHelper
   @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")
   )
   ```

   ```Razor TagHelper
   @page
   @model IndexModel
   @using Kendo.Mvc.UI

   <kendo-datasource name="dataSource1" type="DataSourceTagHelperType.Ajax" page-size="20">
       <transport>
           <read url="@Url.Page("Index", "Read")" data="forgeryToken" />
       </transport>
       <schema>
           <model id="OrderID">
               <fields>
                   <field name="OrderID" type="number"></field>
                   <field name="Freight" type="number"></field>
                   <field name="OrderDate" type="date"></field>
                   <field name="ShipName" type="string"></field>
                   <field name="ShipCity" type="string"></field>
               </fields>
           </model>
       </schema>
   </kendo-datasource>

   <kendo-pager name="pager" datasource-id="dataSource1" button-count="5" page-sizes="true">
   </kendo-pager>

   <kendo-grid name="grid" datasource-id="dataSource1" height="440">
       <columns>
           <column field="OrderID" width="100">
               <filterable enabled="false" />
           </column>
           <column field="Freight" width="100" />
           <column field="OrderDate" width="140" format="{0:MM/dd/yyyy}" />
           <column field="ShipName" />
           <column field="ShipCity" width="150" />
       </columns>
       <sortable enabled="true" />
       <scrollable enabled="true" />
       <filterable enabled="true" />
   </kendo-grid>
   ```

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.

   ```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>
   ```

4. Within the `cshtml.cs` file, add a handler method for the Read data operation.

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

   ```C# Model
   public class OrderViewModel
   {
       public int OrderID { get; set; }
       public decimal? Freight { get; set; }
       public string ShipCity { get; set; }
       public string ShipName { get; set; }
   }
   ```

## See Also

* [Using Telerik UI for ASP.NET Core in Razor Pages](https://docs.telerik.com/aspnet-core/getting-started/razor-pages#using-telerik-ui-for-aspnet-core-in-razor-pages)
* [Client-Side API of the Pager](https://docs.telerik.com/kendo-ui/api/ui/pager)
* [Server-Side HtmlHelper API of the Pager](https://www.telerik.com/aspnet-core-ui/documentation/api/pager)
* [Server-Side TagHelper API of the Pager](https://www.telerik.com/aspnet-core-ui/documentation/api/taghelpers/pager)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
