I am also using entity framework code first model to get the list of countries and its states.

Model

In this model class I have created a property CountryID (hold selected value of country), State (hold selected value of state), Countries and States (hold list of available country and its states).

  1. public class Model
  2. {
  3. public int ? CountryID
  4. {
  5. get;
  6. set;
  7. }
  8. public int ? StateID
  9. {
  10. get;
  11. set;
  12. }
  13. public IEnumerable < Country > Countries
  14. {
  15. get;
  16. set;
  17. }
  18. public IEnumerable < State > States
  19. {
  20. get;
  21. set;
  22. }
  23. }
Add some sample data in database and initialize it when database has been created:
  1. public class ModelInitializer: DropCreateDatabaseIfModelChanges < ModelDbContext >
  2. {
  3. protected override void Seed(ModelDbContext context)
  4. {
  5. var countries = new List < Country >
  6. {
  7. new Country
  8. {
  9. Name = "India"
  10. },
  11. new Country
  12. {
  13. Name = "USA"
  14. },
  15. new Country
  16. {
  17. Name = "South Africa"
  18. },
  19. new Country
  20. {
  21. Name = "Australlia"
  22. },
  23. };
  24. var states = new List < State >
  25. {
  26. new State
  27. {
  28. Name = "Delhi", Country = countries.Single(m => m.Name == "India")
  29. },
  30. new State
  31. {
  32. Name = "Mumbai", Country = countries.Single(m => m.Name == "India")
  33. },
  34. new State
  35. {
  36. Name = "California", Country = countries.Single(m => m.Name == "USA")
  37. },
  38. new State
  39. {
  40. Name = "Newyork", Country = countries.Single(m => m.Name == "USA")
  41. },
  42. new State
  43. {
  44. Name = "Capetown", Country = countries.Single(m => m.Name == "South Africa")
  45. },
  46. new State
  47. {
  48. Name = "Bolavia", Country = countries.Single(m => m.Name == "South Africa")
  49. },
  50. new State
  51. {
  52. Name = "Sydney", Country = countries.Single(m => m.Name == "Australlia")
  53. },
  54. new State
  55. {
  56. Name = "Melbourne", Country = countries.Single(m => m.Name == "Australlia")
  57. },
  58. };
  59. countries.ForEach(m => context.Countries.Add(m));
  60. states.ForEach(m => context.States.Add(m));
  61. }
  62. }
Add a connection string in the web.config file under the configuration tag:
  1. <connectionStrings>
  2. <add name="ModelDbContext" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=CountryDb; Integrated Security=true;" providerName="System.Data.SqlClient"/>
  3. </connectionStrings>
Modify the Global.asax file as shown below:
  1. protected void Application_Start()
  2. {
  3. Database.SetInitializer(new CascadingDropDown.Models.ModelInitializer());
  4. AreaRegistration.RegisterAllAreas();
  5. WebApiConfig.Register(GlobalConfiguration.Configuration);
  6. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  7. RouteConfig.RegisterRoutes(RouteTable.Routes);
  8. }
Create a HomeController and add the following action methods. In this controller, Index action returns a Model type object to Index view by holding the list of countries from the database. SelectCountry action returns a list of states on the basis of country id.
  1. ModelDbContext db = new ModelDbContext();
  2. public ActionResult Index()
  3. {
  4. Model model = new Model
  5. {
  6. Countries = db.Countries.ToList()
  7. };
  8. return View(model);
  9. }
  10. [HttpPost]
  11. public virtual ActionResult SelectCountry(int ? countryid)
  12. {
  13. var states = countryid.HasValue ? db.Countries.FirstOrDefault(m => m.CountryID == countryid).States : null;
  14. Model model = new Model
  15. {
  16. CountryID = countryid,
  17. Countries = db.Countries.ToList(),
  18. States = states
  19. };
  20. if (Request.IsAjaxRequest()) return PartialView("_States", model);
  21. else return View("Index", model);
  22. }
Add an Index View and modify this as given below:
  1. @model CascadingDropDown.Models.Model
  2. @{
  3. Layout = null;
  4. }
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <meta name="viewport" content="width=device-width" />
  9. <title>Index</title>
  10. <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
  11. <script type="text/javascript">
  12. $(document).ready(function () {
  13. $('input[type=submit]').hide();
  14. $('#CountryID').change(function () {
  15. $(this).parents('form').submit();
  16. return false;
  17. });
  18. $("form[action$='SelectCountry']").submit(function () {
  19. $.ajax({
  20. url: $(this).attr('action'),
  21. type: 'post',
  22. data: $(this).serialize(),
  23. success: function (response) {
  24. $('#states').html(response);
  25. }
  26. });
  27. return false;
  28. });
  29. });
  30. </script>
  31. </head>
  32. <body>
  33. <div>
  34. @using (Html.BeginForm("SelectCountry", "Home"))
  35. {
  36. <fieldset>
  37. <legend>Countries</legend>
  38. @Html.DropDownListFor(m => m.CountryID, new SelectList(Model.Countries,"CountryID", "Name"), "[Please select a Country]")
  39. <input type="submit" value="Select" />
  40. </fieldset>
  41. }
  42. </div>
  43. <div id="states">
  44. @Html.Partial("_States", Model)
  45. </div>
  46. </body>
  47. </html>
Add a _States partial view:
  1. @model CascadingDropDown.Models.Model
  2. <fieldset>
  3. @if (Model.States != null && Model.States.Count() > 0)
  4. {
  5. <legend>States</legend>
  6. @Html.HiddenFor(m => m.CountryID);
  7. @Html.DropDownListFor(m => m.StateID, new SelectList(Model.States, "StateID","Name"), "[Please select a state]")
  8. }
  9. else
  10. {
  11. <legend>No states available</legend>
  12. }
  13. </fieldset>

Now run an application. It will something look like below:

Run Application

When JavaScript is turned off a select button will also be displayed in the UI in order to populate the states.

Country

When you select the country, it will populate all states in the state DropDown on the basis of selected country. You can see in the following screenshot.

Select country

Thanks for reading this article. You can enter your valuable comments and suggestion to improve this article in the comment box.

Read more articles on ASP.NET: