Example of Cascading in ASP.NET MVC ComboBox Control
This sample demonstrates the cascading functionalities of the ComboBox. Choose a country from countries ComboBox, then respective states will be loaded in the second ComboBox and the same has to done between states and cities ComboBox.
@Html.UiComboBox("country", new UiComboBoxOptions { Placeholder = "Select a country", PopupHeight = "auto", AllowCustom = false, DataSource = (IEnumerable<DropdownsDemoController.Country>)ViewData["country"], Fields = new UiDropDownFields { Text = "CountryName", Value = "CountryId" } })
@Html.UiComboBox("state", new UiComboBoxOptions { Placeholder = "Select a state", PopupHeight = "auto", AllowCustom = false, DataSource = (IEnumerable<DropdownsDemoController.State>)ViewData["state"], Fields = new UiDropDownFields { Text = "StateName", Value = "StateId" }, CascadeFrom = "country", CascadeField = "CountryId" })
@Html.UiComboBox("city", new UiComboBoxOptions { Placeholder = "Select a city", PopupHeight = "auto", AllowCustom = false, DataSource = (IEnumerable<DropdownsDemoController.Cities>)ViewData["cities"], Fields = new UiDropDownFields { Text = "CityName", Value = "CityId" }, CascadeFrom = "state", CascadeField = "StateId" })
<style>
.control-wrapper { margin: 0 auto; width: 250px; }
.control-section .padding-top { padding-top: 35px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div class="control-wrapper">
<div class="padding-top">
@Html.UiComboBox("country", new UiComboBoxOptions { Placeholder = "Select a country", PopupHeight = "auto", AllowCustom = false, DataSource = (IEnumerable<DropdownsDemoController.Country>)ViewData["country"], Fields = new UiDropDownFields { Text = "CountryName", Value = "CountryId" } })
</div>
<div class="padding-top">
@Html.UiComboBox("state", new UiComboBoxOptions { Placeholder = "Select a state", PopupHeight = "auto", AllowCustom = false, DataSource = (IEnumerable<DropdownsDemoController.State>)ViewData["state"], Fields = new UiDropDownFields { Text = "StateName", Value = "StateId" }, CascadeFrom = "country", CascadeField = "CountryId" })
</div>
<div class="padding-top">
@Html.UiComboBox("city", new UiComboBoxOptions { Placeholder = "Select a city", PopupHeight = "auto", AllowCustom = false, DataSource = (IEnumerable<DropdownsDemoController.Cities>)ViewData["cities"], Fields = new UiDropDownFields { Text = "CityName", Value = "CityId" }, CascadeFrom = "state", CascadeField = "StateId" })
</div>
</div>
</div>
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/Cascading.cshtml; it draws what ViewData holds.
public ActionResult Cascading()
{
ViewData["state"] = new State().StateList();
ViewData["country"] = new Country().CountryList();
ViewData["cities"] = new Cities().CitiesList();
ViewData["popupHeight"] = "auto";
return View();
}
public class State
{
public string StateName { get; set; }
public string CountryId { get; set; }
public string StateId { get; set; }
public List<State> StateList()
{
List<State> state = new List<State>();
state.Add(new State() { StateName = "New York", CountryId = "1", StateId = "101" });
state.Add(new State() { StateName = "Queensland", CountryId = "2", StateId = "104" });
state.Add(new State() { StateName = "Tasmania ", CountryId = "2", StateId = "105" });
state.Add(new State() { StateName = "Victoria", CountryId = "2", StateId = "106" });
state.Add(new State() { StateName = "Virginia ", CountryId = "1", StateId = "102" });
state.Add(new State() { StateName = "Washington", CountryId = "1", StateId = "103" });
return state;
}
}
public class Country
{
public string CountryName { get; set; }
public string CountryId { get; set; }
public List<Country> CountryList()
{
List<Country> country = new List<Country>();
country.Add(new Country() { CountryName = "Australia", CountryId = "2" });
country.Add(new Country() { CountryName = "United States", CountryId = "1" });
return country;
}
}
public class Cities
{
public string CityName { get; set; }
public string StateId { get; set; }
public int CityId { get; set; }
public List<Cities> CitiesList()
{
List<Cities> cities = new List<Cities>();
cities.Add(new Cities() { CityName = "Aberdeen", StateId = "103", CityId = 207 });
cities.Add(new Cities() { CityName = "Alexandria", StateId = "102", CityId = 204 });
cities.Add(new Cities() { CityName = "Albany", StateId = "101", CityId = 201 });
cities.Add(new Cities() { CityName = "Beacon ", StateId = "101", CityId = 202 });
cities.Add(new Cities() { CityName = "Brisbane ", StateId = "104", CityId = 211 });
cities.Add(new Cities() { CityName = "Cairns", StateId = "104", CityId = 212 });
cities.Add(new Cities() { CityName = "Colville ", StateId = "103", CityId = 208 });
cities.Add(new Cities() { CityName = "Devonport", StateId = "105", CityId = 215 });
cities.Add(new Cities() { CityName = "Emporia", StateId = "102", CityId = 206 });
cities.Add(new Cities() { CityName = "Geelong", StateId = "106", CityId = 218 });
cities.Add(new Cities() { CityName = "Hampton ", StateId = "102", CityId = 205 });
cities.Add(new Cities() { CityName = "Healesville ", StateId = "106", CityId = 217 });
cities.Add(new Cities() { CityName = "Hobart", StateId = "105", CityId = 213 });
cities.Add(new Cities() { CityName = "Launceston ", StateId = "105", CityId = 214 });
cities.Add(new Cities() { CityName = "Lockport", StateId = "101", CityId = 203 });
cities.Add(new Cities() { CityName = "Melbourne", StateId = "106", CityId = 216 });
cities.Add(new Cities() { CityName = "Pasco", StateId = "103", CityId = 209 });
cities.Add(new Cities() { CityName = "Townsville", StateId = "104", CityId = 210 });
return cities;
}
}
}
}
@* 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 Cascading ComboBox is the series of ComboBox, where the value of one ComboBox depends on the another ComboBox value. This can be configured by using the change event of parent ComboBox. Within that change event handler, you should load the data to child ComboBox based on the selected value of parent ComboBox.