Getting Started with ASP.NET Core ComboBox Control

23 Jul 20264 minutes to read

This section briefly explains how to include the ASP.NET Core ComboBox control in your ASP.NET Core application using Visual Studio.

Create an ASP.NET Core Web App with Razor pages

Create an ASP.NET Core Web App using Visual Studio via Microsoft Templates or the ASP.NET Core Extension. For detailed instructions, refer to the ASP.NET Core Web App Getting Started documentation.

Install the required ASP.NET Core packages

To add ASP.NET Core ComboBox control in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for and install the Syncfusion.AspNetCore.DropDowns and Syncfusion.AspNetCore.Themes packages. All Syncfusion ASP.NET Core packages are available on nuget.org. See the NuGet packages topic for details.

Alternatively, you can install the same packages using the Package Manager Console with the following command.

Install-Package Syncfusion.AspNetCore.DropDowns -Version 34.1.29
Install-Package Syncfusion.AspNetCore.Themes -Version 34.1.29

Add ASP.NET Core Tag Helpers

After the packages are installed, open the ~/Pages/_ViewImports.cshtml file and import the Syncfusion.AspNetCore.Base and Syncfusion.AspNetCore.DropDowns Tag Helpers.

@addTagHelper *, Syncfusion.AspNetCore.Base
@addTagHelper *, Syncfusion.AspNetCore.DropDowns

Add stylesheet and script resources

The theme stylesheet and script can be referenced from CDN. Include the stylesheet and script references inside the <head> of ~/Pages/Shared/_Layout.cshtml.

<head>
    ...
    @* ASP.NET Core controls styles *@
    <link rel="stylesheet" href="_content/Syncfusion.AspNetCore.Themes/styles/fluent2.css" />
    @* ASP.NET Core controls scripts *@
    <script src="_content/Syncfusion.AspNetCore.DropDowns/scripts/sf-combo-box.min.js"></script>
</head>

Register the Script Manager

Open the ~/Pages/Shared/_Layout.cshtml file and register the script manager <ejs-scripts> at the end of the <body> element as follows.

<body>
    ...
    @*  ASP.NET Core Script Manager *@
    <ejs-scripts></ejs-scripts>
</body>

Add ASP.NET Core ComboBox control

Add the ASP.NET Core ComboBox control in the ~/Pages/Index.cshtml file.

@{
    var data = new string[] { "Badminton", "Basketball", "Cricket", "Football", "Golf", "Gymnastics", "Hockey", "Tennis" };
}

<ejs-combobox id="games" dataSource="data" placeholder="Select a game">
</ejs-combobox>

Run the application

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The ASP.NET Core ComboBox control will render in your default web browser.

ASP.NET Core ComboBox Control

Binding data source

After initializing, populate the ASP.NET Core ComboBox with data using the dataSource property with an array of string values.

@{
    ...
    var data = new string[] { "Badminton", "Basketball", "Cricket", "Football", "Golf", "Gymnastics", "Hockey", "Tennis" };
}

<ejs-combobox id="games" dataSource="data" placeholder="Select a Country" popupHeight="220px">
</ejs-combobox>

Custom values

The ASP.NET Core ComboBox allows users to enter custom values that are not present in the predefined set of values. By default, this support is enabled by the allowCustom property. In this case, both the text field and value field are considered the same. The custom value will be sent to the post back handler when a form is submitted.

@{
    ...
    var data = new string[] { "Badminton", "Basketball", "Cricket", "Football", "Golf", "Gymnastics", "Hockey", "Tennis" };
}

<ejs-combobox id="games" allowCustom="true" dataSource="data" placeholder="Select a Country" popupHeight="220px">
</ejs-combobox>

Configure the popup list

By default, the width of the popup list automatically adjusts according to the ASP.NET Core ComboBox input element’s width, and the height of the popup list is 300px.

The height and width of the popup list can also be customized using the popupHeight and popupWidth properties respectively.

@{
    ...
    var data = new string[] { "Badminton", "Basketball", "Cricket", "Football", "Golf", "Gymnastics", "Hockey", "Tennis" };
}

<ejs-combobox id="games" dataSource="data" popupWidth="300px" placeholder="Select a game" popupHeight="220px">
</ejs-combobox>

NOTE

View Sample in GitHub.

See also