Cascading Dropdown List in ASP.NET Core MVC
Problem definition: In the image below there are three drop-down lists: Country, State and City. Our requirement is to filter the states based on the selected country, and the cities based on the selected state.
Implementation: To implement the drop-down lists, follow the steps below.
- Create a model class for Country.
- Create a model class for State.
- Create a model class for City.
- Create a controller named Cascade.
- Create an action method named Cascade inside the controller.
- Create three methods: one to get the countries from the database, one to get the states for a country, and one to get the cities for a state.
- Create a JavaScript file named Cascade.js for the JavaScript code.
- Add these three model classes to the DbContext class and then run the migration commands.
In the implementation phase, we create three model classes for the drop-down lists. Below is the code for the Country model class.
public class Country
{
[Key]
public int countryId { get; set; }
public string countryName { get; set; }
}
Below is the model class for State. It has three properties: StateId, StateName and Country (the country the state belongs to).
public class State
{
[Key]
public int stateId { get; set; }
public string stateName { get; set; }
public Country country { get; set; }
}
Below is the model class for City. It has three properties: CityId, CityName and State (the state the city belongs to).
public class City
{
[Key]
public int cityId { get; set; }
public string cityName { get; set; }
public State state { get; set; }
}
Now create a DbContext class named demoDBcontext and add the three DbSets below. They generate the tables in the database when you run the migration commands.
public DbSet<Country> Countries { get; set; }
public DbSet<State> States { get; set; }
public DbSet<City> Cities { get; set; }
Now create a controller named Cascade and write the action methods below in it.
GetCountry() fetches the list of countries from the database and returns it as a JsonResult. It is used in the JavaScript file to fill the Country drop-down list.
GetState(int countryId) fetches the list of states for the countryId selected in the Country drop-down list and returns it as a JsonResult. It is used in the JavaScript file to fill the State drop-down list.
GetCities(int stateId) fetches the list of cities for the stateId selected in the State drop-down list and returns it as a JsonResult. It is used in the JavaScript file to fill the City drop-down list.
public IActionResult Cascade()
{
return View();
}
public JsonResult GetCountry()
{
var contryList = _db.Countries.ToList();
return new JsonResult(contryList);
}
public JsonResult GetState(int countryId)
{
var stateList = _db.States.Where(p=>p.country.countryId==countryId).ToList();
return new JsonResult(stateList);
}
public JsonResult GetCities(int stateId)
{
var cityList = _db.Cities.Where(p => p.state.stateId == stateId).ToList();
return new JsonResult(cityList);
}
Now create a view for the Cascade action method and write the code below.
Now create a JavaScript file named cascade.js and write the code below.
GetCountryList() fetches the countries by calling the URL '/Cascade/GetCountry' and appends the returned list to the Country drop-down list.$('#country').change(function () runs when the selected country changes and fills the State drop-down list. The State list works the same way for the cities.
$(document).ready(function () {
GetCountryList();
$('#state').attr('disabled', true);
$('#city').attr('disabled', true);
$('#country').change(function () {
$('#state').attr('disabled', false);
var cid = $(this).val();
$('#state').empty();
$('#state').append('<option>--select state--</option>');
$.ajax({
url: '/Cascade/GetState?countryId='+cid,
success: function (result) {
$.each(result, function (i, data) {
$('#state').append('<option value=' + data.stateId + '>' + data.stateName + '</option>');
});
}
});
});
$('#state').change(function () {
$('#city').attr('disabled', false);
var sid = $(this).val();
$('#city').empty();
$('#city').append('<option>--select city--</option>');
$.ajax({
url: '/Cascade/GetCities?stateId=' + sid,
success: function (result) {
$.each(result, function (i, data) {
$('#city').append('<option value=' + data.cityId + '>' + data.cityName + '</option>');
});
}
});
});
});
function GetCountryList() {
$.ajax({
url: '/Cascade/GetCountry',
success: function (result) {
$.each(result, function (i, data) {
$('#country').append('<option value=' + data.countryId + '>' + data.countryName +'</option>');
});
}
});
}
Now run the application. You can filter the states by country and the cities by state.