New to Telerik UI for ASP.NET Core? Download free 30-day trial

Filter 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 Filter for ASP.NET Core in Razor Pages applications.

This article describes how to configure the Filter component in a Razor Pages scenario.

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

Getting Started

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.

            .Ajax(t =>
                t.Model(model => model.Id(p => p.CustomerID));
            .Fields(f =>
            .FilterExpression(f => {
                f.Add(p => p.Position).IsEqualTo("Sales Representative");
            var InitialExpression = new CompositeFilterDescriptor();
            InitialExpression.FilterDescriptors.Add(new FilterDescriptor { Member = "Position", Value = "Sales Representative", Operator = FilterOperator.IsEqualTo });
        <kendo-datasource name="dataSource1" type="DataSourceTagHelperType.Ajax" page-size="20">
                <read type="post" url="/Index?handler=Customers" data="forgeryToken" />
                <model id="CustomerID"></model>
        <kendo-filter name="filter" apply-button="true" expression-preview="true" 
                <filter-field name="CustomerID"></filter-field>
                <filter-field name="Position"></filter-field>
                <filter-field name="CompanyName"></filter-field>
                <filter-field name="Country"></filter-field>
  2. Add an AntiForgeryToken at the top of the page.

        @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
  3. Send the AntiForgeryToken with the Read request.

            function forgeryToken() {
                return kendo.antiForgeryTokens();

    Additional parameters can also be supplied.

            function forgeryToken() {
                return {
                    __RequestVerificationToken: kendo.antiForgeryTokens().__RequestVerificationToken,
                    additionalParameter: "test"
  4. Within the cshtml.cs file, add a handler method for the Read operation that returns the dataset.

        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() { 
                        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));
        public class CustomerViewModel
            public int CustomerID { get; set; }
            public string ContactName { get; set; }
            public string Position { get; set; }
            public string CompanyName { get; set; }
            public string Country { get; set; }

See Also

In this article