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

Filter in Razor Pages

Updated on Dec 10, 2025

This article describes how to seamlessly integrate and configure the Telerik UI Filter 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.

Binding to Remote Data

To bind the Filter DataSource to a data set received from a remote endpoint within a Razor Pages application, follow the next steps:

  1. Specify the Read request URL in the DataSource configuration. The URL must refer to the method name in the PageModel.

    Razor
        @page
        @IndexModel
    
        @(Html.Kendo().DataSource<CustomerViewModel>()
            .Name("dataSource1")
            .Ajax(t =>
            {
                t.Read(r=>r.Url("/Index?handler=Customers").Data("forgeryToken"));
                t.Model(model => model.Id(p => p.CustomerID));
                t.PageSize(20);
            })
        )
    
        @(Html.Kendo().Filter<CustomerViewModel>()
            .Name("filter")
            .ApplyButton(true)
            .ExpressionPreview(true)
            .Fields(f =>
            {
                f.Add(p=>p.CustomerID);
                f.Add(p=>p.Position);
                f.Add(p=>p.CompanyName);
                f.Add(p=>p.Country);
            })
            .FilterExpression(f => {
                f.Add(p => p.Position).IsEqualTo("Sales Representative");
            })
            .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.

    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 operation that returns the dataset.

    C#
        public static IList<CustomerViewModel> Customers;
        public static IList<string> Countries = new List<string>() {"UK", "Germany", "Italy", "Venezuela", "China", "Bulgaria", "USA" };
        public static IList<string> Positions = new List<string>() { "Sales Agent", "Sales Representative",  "Owner",  "Order Administrator", "Marketing Manager", "Accounting Manager" };
    
        public void OnGet()
        {
            if (Customers == null)
            {
                // Populate the "Customers" collection with data.
                Customers = new List<CustomerViewModel>();
                var rand = new Random();
                for (int i = 1; i < 40; i++)
                {
                    Customers.Add(new CustomerViewModel() { 
                        CustomerID=i,
                        CompanyName="Company " + i,
                        Position = Positions[rand.Next(0,5)],
                        Country = Countries[rand.Next(0, 6)],
                    });
                }
            }
        }
    
        public JsonResult OnPostCustomers([DataSourceRequest]DataSourceRequest request)
        {
            return new JsonResult(Customers.ToDataSourceResult(request));
        }

For the complete project, refer to the Filter in Razor Pages example.

See Also