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

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

This article describeshow to configure the DropDownList component in a Razor Pages scenario.

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

Getting Started

The DataSource component offers the most versatile data binding approach. To connect the DropDownList to a dataset retrieved from a remote endpoint in a Razor Pages application, proceed with the following steps:

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

    Razor
    @(Html.Kendo().DropDownList()
        .Name("ordersDDL")
        .DataTextField("ShipName")
        .DataValueField("OrderID")
        .DataSource(source =>
        {
            source.Read(read => read
                .Url("/Index?handler=Read").Data("forgeryToken"));
        })
    )
  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. For example, when the server filtering of the DropDownList is enabled, send the filter value along with the antiforgery token to the server using the JavaScript handler specified in the Data() option.

    Razor
        @page
        @model IndexModel
        <div>
            @(Html.Kendo().DropDownList()
                .Name("ordersDDL")
                .DataTextField("ShipName")
                .DataValueField("OrderID")
                .AutoBind(false)
                .Filter(FilterType.Contains)
                .MinLength(3)
                .DataSource(source =>
                {
                    source.Read(read => read
                        .Url("/Index?handler=Read").Data("dataFunction"))
                        .ServerFiltering(true);
                })
            )
        </div>
  4. Within the cshtml.cs file, add a handler method for the Read operation that returns the dataset.

    C#
        public class IndexModel : PageModel
        {
            public JsonResult OnGetRead(string filterValue)
            {
                var dropdownListData = new List<OrderViewModel>();
                // Populate the collection with the DropDownList data.
                return new JsonResult(dropdownListData);
            }
        }

    When the server filtering is enabled, intercept the filter value sent through the dataFunction handler in the Read method and filter the data on the server before returning it to the DropDownList.

    C#
        public class IndexModel : PageModel
        {
            public JsonResult OnGetRead(string filterValue)
            {
                var dropdownListData = new List<OrderViewModel>();
                // Populate the collection with the DropDownList data.
    
                if (filterValue != null)
                {
                    var filteredData = dropdownListData.Where(p => p.ShipName.Contains(filterValue));
                    return new JsonResult(filteredData);
                }
                return new JsonResult(dropdownListData);
            }
        }

Binding the DropDownList to a PageModel Property

To bind the DropDownList to a property from the PageModel, follow the next steps:

  1. Add a property to the PageModel that must bind to the DropDownList.

    C#
    public class IndexModel : PageModel
    {
        [BindProperty]
        public int OrderID { get; set; }
    
        public void OnGet()
        {
            OrderID = 2; // Assign a value to the "OrderID" property, if needed.
        }
    }
  2. Declare the PageModel at the top of the page.

    Razor
        @page
        @model IndexModel
  3. Bind the DropDownList to the property using the DropDownListFor() configuration.

    Razor
        @page
        @model IndexModel
    
        @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
        @Html.AntiForgeryToken()
        
        @(Html.Kendo().DropDownListFor(m => m.OrderID)  
            .DataTextField("ShipName")
            .DataValueField("OrderID")
            .DataSource(source =>
            {
                source.Read(read => read
                    .Url("/Index?handler=Read").Data("forgeryToken"));
            })
        )

See Also