ComboBox 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 ComboBox for ASP.NET Core in Razor Pages applications.
This article describes how to configure the ComboBox component in a Razor Pages scenario.
For the complete project, refer to the ComboBox in Razor Pages example.
Getting Started
The DataSource component offers the most versatile data binding approach. To connect the ComboBox to a data set retrieved from a remote endpoint in a Razor Pages application, proceed with the following steps:
-
Specify the Read request URL in the
DataSourceconfiguration. The URL must refer to the method name in thePageModel.Razor@page @model IndexModel <div> @(Html.Kendo().ComboBox() .Name("combobox") .DataTextField("ShipName") .DataValueField("OrderID") .DataSource(source => { source.Read(read => read .Url("/Index?handler=Read").Data("forgeryToken")); }) ) </div> -
Add an
AntiForgeryTokenat the top of the page.C#@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() -
Send the
AntiForgeryTokenwith the Read request.JS<script> function forgeryToken(e) { return kendo.antiForgeryTokens(); } </script>Additional parameters can also be supplied. For example, when the server filtering of the ComboBox 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().ComboBox() .Name("combobox") .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> -
Within the
cshtml.csfile, add a handler method for the Read operation that returns the dataset.C#public class IndexModel : PageModel { public JsonResult OnGetRead() { var comboBoxData = new List<OrderViewModel>(); // Populate the collection with the ComboBox data. return new JsonResult(comboBoxData); } }When the server filtering is enabled, intercept the filter value sent through the
dataFunctionhandler in the Read method and filter the data on the server before returning it to the ComboBox.C#public class IndexModel : PageModel { public JsonResult OnGetRead(string filterValue) { var comboBoxData = new List<OrderViewModel>(); // Populate the collection with the ComboBox data. if (filterValue != null) { var filteredData = comboBoxData.Where(p => p.ShipName.Contains(filterValue)); return new JsonResult(filteredData); } return new JsonResult(comboBoxData); } }
Binding the ComboBox to a PageModel Property
To bind the ComboBox to a property from the PageModel, follow the next steps:
-
Add a property to the
PageModelthat must bind to the ComboBox.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. } } -
Declare the
PageModelat the top of the page.Razor@page @model IndexModel -
Bind the ComboBox to the property using the
ComboBoxFor()configuration.Razor@page @model IndexModel @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() @(Html.Kendo().ComboBoxFor(m => m.OrderID) .DataTextField("ShipName") .DataValueField("OrderID") .DataSource(source => { source.Read(read => read .Url("/Index?handler=Read").Data("forgeryToken")); }) )