Example of Cascading in ASP.NET MVC DropDownList Control
This sample demonstrates the cascading functionalities of the DropDownList. Choose a country from the countries DropDownList, then respective states will be loaded in the second DropDownList and the same has to be done between states and cities DropDownList.
@Html.UiDropDownList("country", new UiDropDownListOptions { Placeholder = "Select a country", PopupHeight = "auto", DataSource = (IEnumerable<DropdownsDemoController.Country>)ViewData["country"], Fields = new UiDropDownFields { Text = "CountryName", Value = "CountryId" } })
@Html.UiDropDownList("state", new UiDropDownListOptions { Placeholder = "Select a state", PopupHeight = "auto", DataSource = (IEnumerable<DropdownsDemoController.State>)ViewData["state"], Fields = new UiDropDownFields { Text = "StateName", Value = "StateId" }, CascadeFrom = "country", CascadeField = "CountryId" })
@Html.UiDropDownList("city", new UiDropDownListOptions { Placeholder = "Select a city", PopupHeight = "auto", 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.UiDropDownList("country", new UiDropDownListOptions { Placeholder = "Select a country", PopupHeight = "auto", DataSource = (IEnumerable<DropdownsDemoController.Country>)ViewData["country"], Fields = new UiDropDownFields { Text = "CountryName", Value = "CountryId" } })
</div>
<div class="padding-top">
@Html.UiDropDownList("state", new UiDropDownListOptions { Placeholder = "Select a state", PopupHeight = "auto", DataSource = (IEnumerable<DropdownsDemoController.State>)ViewData["state"], Fields = new UiDropDownFields { Text = "StateName", Value = "StateId" }, CascadeFrom = "country", CascadeField = "CountryId" })
</div>
<div class="padding-top">
@Html.UiDropDownList("city", new UiDropDownListOptions { Placeholder = "Select a city", PopupHeight = "auto", 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 DropDownListController : Controller
{
// The page is Views/DropDownList/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 DropDownList is the series of DropDownList, where the value of one DropDownList depends on the another DropDownList value. This can be configured by using the change event of parent DropDownList. Within that change event handler, you should load the data to child DropDownList based on the selected value of parent DropDownList.
In this sample, if a country is selected from countries DropDownList, the respective states will be loaded in the second DropDownList and the same has to be done between states and cities DropDownList.