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

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:

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

        @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>
    
        @page
        @model IndexModel
        @addTagHelper *, Kendo.Mvc
    
        <div>
            <kendo-combobox name="combobox"
                datatextfield="ShipName" 
                datavaluefield="OrderID">
                <datasource>
                    <transport>
                        <read url="/Index?handler=Read" data="forgeryToken"/>
                    </transport>
                </datasource>
            </kendo-combobox>
        </div>
    
  2. Add an AntiForgeryToken at the top of the page.

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

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

        @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>
    
        @page
        @model IndexModel
        @addTagHelper *, Kendo.Mvc
    
        <div>
            <kendo-combobox name="combobox" auto-bind="false" 
                datatextfield="ShipName" 
                datavaluefield="OrderID" 
                min-length="3"
                filter="FilterType.Contains">
                <datasource server-filtering="true">
                    <transport>
                        <read url="/Index?handler=Read" data="dataFunction"/>
                    </transport>
                </datasource>
            </kendo-combobox>
        </div>
    
        <script>
            function dataFunction(e) {
                var filterValue = '';
                if (e.filter.filters[0]) {
                    filterValue = e.filter.filters[0].value;
                }
    
                return {
                    __RequestVerificationToken: kendo.antiForgeryTokens().__RequestVerificationToken,
                    filterValue: filterValue
                };
            }
        </script>
    
  4. Within the cshtml.cs file, add a handler method for the Read operation that returns the dataset.

        public class IndexModel : PageModel
        {
            public JsonResult OnGetRead()
            {
                var comboBoxData = new List<OrderViewModel>();
                // Populate the collection with the ComboBox data.
                return new JsonResult(comboBoxData);
            }
        }
    
        public class OrderViewModel
        {
            public int OrderID { get; set; }
    
            public string ShipName { get; set; }
        }
    

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

        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:

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

        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.

        @page
        @model IndexModel
    
  3. Bind the ComboBox to the property using the ComboBoxFor() configuration.

        @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"));
            })
        )
    
        @page
        @model IndexModel
    
        @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
        @Html.AntiForgeryToken()
        @addTagHelper *, Kendo.Mvc
    
        <kendo-combobox for="OrderID"
            datatextfield="ShipName" 
            datavaluefield="OrderID">
            <datasource>
                <transport>
                    <read url="/Index?handler=Read" data="forgeryToken"/>
                </transport>
            </datasource>
        </kendo-combobox>
    
        <script>
            function forgeryToken(e) {
                return kendo.antiForgeryTokens();
            }
        </script>
    

See Also

In this article