Example of Filtering in ASP.NET MVC DropDownList Control
This sample demonstrates the filtering functionalities of the DropDownList. Click the DropDownList element and then type a character in the search box. It will display the filtered list items based on the typed characters.
@Html.UiDropDownList("country", new UiDropDownListOptions
{
Placeholder = "Select a country",
FilterBarPlaceholder = "Search",
AllowFiltering = true,
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
DebounceDelay = 300
})
@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.UiDropDownList("country", new UiDropDownListOptions
{
Placeholder = "Select a country",
FilterBarPlaceholder = "Search",
AllowFiltering = true,
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
DebounceDelay = 300
})
</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 DropDownListController : Controller
{
// The page is Views/DropDownList/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 DropDownList has built-in support to filter the data source, when allowFiltering is enabled. It performs when characters are typed in the search box. In the filtering event, you can filter down the data source and return the resulted data to DropDownList via updateData method to display its list items. The debounce delay, in milliseconds, for filtering items in the DropDownList component can be set using the DebounceDelay property.
This sample illustrates that, query the data source and pass the resulted data to DropDownList through the updateData method in filtering event.