Example of Filtering in ASP.NET MVC ComboBox Control
This sample demonstrates the filtering functionalities of the ComboBox. Type a character in ComboBox element and choose an item from the filtered list based on the typed characters.
@Html.UiComboBox("country", new UiComboBoxOptions
{
Placeholder = "Select a country",
FilterBarPlaceholder = "Search",
AllowFiltering = true,
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
DebounceDelay = 300,
PopupHeight = "270px"
})
@Html.UiNumericTextBox("numeric", new UiNumericTextBoxOptions { Format = "n0", Value = 300, Min = 1, OnChange = "change" })
<style>
.control-wrapper { margin: 0 auto; width: 250px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section" style="height: 400px;">
<div class="control-wrapper">
<div style="padding-top:75px;">
@Html.UiComboBox("country", new UiComboBoxOptions
{
Placeholder = "Select a country",
FilterBarPlaceholder = "Search",
AllowFiltering = true,
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
DebounceDelay = 300,
PopupHeight = "270px"
})
</div>
</div>
</div>
<div class="demo-property-panel" title="Properties">
<div class="demo-property-row">
<label class="example-label">Debounce Delay</label>
@Html.UiNumericTextBox("numeric", new UiNumericTextBoxOptions { Format = "n0", Value = 300, Min = 1, OnChange = "change" })
</div>
</div>
<script>
// Syncfusion: the DropDownList's debounceDelay is the value of the NumericTextBox.
function change(value) {
window.Lumen.get('country').setDebounceDelay(value);
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ComboBoxController : Controller
{
// The page is Views/ComboBox/Filtering.cshtml; it draws what ViewData holds.
public ActionResult Filtering()
{
ViewData["data"] = new CountriesData().CountriesList();
return View();
}
public class CountriesData
{
public string Name { get; set; }
public string Code { get; set; }
public List<CountriesData> CountriesList()
{
List<CountriesData> country = new List<CountriesData>();
country.Add(new CountriesData { Name = "Australia", Code = "AU" });
country.Add(new CountriesData { Name = "Bermuda", Code = "BM" });
country.Add(new CountriesData { Name = "Canada", Code = "CA" });
// ... 16 more rows
return country;
}
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
The ComboBox has built-in support to filter the data source when allowFiltering is enabled. It performs when characters are typed in the search box. In filtering event, you can filter down the data source and return the resulted data to ComboBox via updateData method to display its list items. The debounce delay, in milliseconds, for filtering items in the ComboBox component can be set using the DebounceDelay property.
This sample illustrates that, query the data source and pass the resulted data to ComboBox through the updateData method in filtering event.