In my last article we discussed about different ways to bind dropdown using MVC.

Firstly, we are going to create MVC Solution.

MVC Solution

Select Empty Template and add MVC Folder Reference,

Select Empty Template

Add New Controller in Controller Folder.

Add New Controller

Select MVC 5 Controller – Empty,

Select MVC 5 Controller

Give Conroller Name HomeController,

Conroller

Add a View.

Right click on the Action Name and then Add View,

Action Name and add vew

Add view

Add Model Class for DropDownList.

Add two classes for Country and State.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Cascading_DropDown.Models
  7. {
  8. class Country
  9. {
  10. public int CountryId
  11. {
  12. get;
  13. set;
  14. }
  15. public string CountryName
  16. {
  17. get;
  18. set;
  19. }
  20. }
  21. class State
  22. {
  23. public int StateId
  24. {
  25. get;
  26. set;
  27. }
  28. public string StateName
  29. {
  30. get;
  31. set;
  32. }
  33. public int CountryId
  34. {
  35. get;
  36. set;
  37. }
  38. }
  39. }
Add code for HomeController,
  1. public class HomeController: Controller
  2. {
  3. private List < Courtry > Con;
  4. private List < State > Sta;
  5. public HomeController()
  6. {
  7. Con = new List < Courtry > ()
  8. {
  9. new Courtry()
  10. {
  11. CountryId = 1, CountryName = "INDIA"
  12. },
  13. new Courtry()
  14. {
  15. CountryId = 2, CountryName = "USA"
  16. }
  17. };
  18. Sta = new List < State > ()
  19. {
  20. new State()
  21. {
  22. StateId = 1, StateName = "Telangana", CountryId = 1
  23. },
  24. new State()
  25. {
  26. StateId = 2, StateName = "Delhi", CountryId = 1
  27. },
  28. new State()
  29. {
  30. StateId = 3, StateName = "New Jersy", CountryId = 2
  31. },
  32. new State()
  33. {
  34. StateId = 3, StateName = "Washington D.C", CountryId = 3
  35. }
  36. };
  37. }
  38. public ActionResult Index()
  39. {
  40. ViewBag.Country = new SelectList(Con, "CountryId", "CountryName");
  41. return View();
  42. }
  43. public JsonResult GetState(int CountryId)
  44. {
  45. return Json(Sta.Where(s => s.CountryId == CountryId), JsonRequestBehavior.AllowGet);
  46. }
  47. }
The View Code looks like the following,
  1. @{
  2. ViewBag.Title = "Index";
  3. }
  4. < div style = "margin-top:10px;" >
  5. < table >
  6. < tr >
  7. < td > Select Country < /td> < td >
  8. @Html.DropDownList("Country", ViewBag.Country as List < SelectListItem > , "Select Country", new
  9. {
  10. @id = "ddlCountry", @class = "form-control"
  11. }) < /td>
  12. < /tr> < tr >
  13. < td >
  14. Select State < /td> < td >
  15. @Html.DropDownList("State", new SelectList(string.Empty), "Select State", new
  16. {
  17. @id = "ddlSate", @class = "form-control"
  18. }) < /td> < /tr> < /table>
  19. < /div> < script src = "~/scripts/jquery-1.10.2.js" > < /script> < script type = "text/javascript" >
  20. $(document).ready(function()
  21. {
  22. $("#ddlCountry").change(function()
  23. {
  24. var SelecedVal = $(this).val();
  25. $("#ddlSate").html('');
  26. $("#ddlSate").append($("<option></option>").attr("value", '')
  27. .text('Select State'));
  28. if (SelecedVal != '')
  29. {
  30. $.get('/Home/GetState/',
  31. {
  32. "CountryId": SelecedVal
  33. }).success(function(data)
  34. {
  35. $.each(data, function(index, item)
  36. {
  37. $("#ddlSate").append($("<option></option>").attr("value", item.StateId)
  38. .text(item.StateName));
  39. });
  40. })
  41. }
  42. });
  43. })
  44. < /script>
script

JQuery Code

JQuery Code

The output should be like the following,

Output