For this I will create a new MVC 4 application.

Open Visual Studio 2012 then select "File" -> "New" -> "Project..."



Now add a new ADO.NET Entity data model.



Enter your SQL Server credentials here.







Now add a new Controller.



Now do the following code here in Employee Controller:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using CascadingDropDownInMVC4.Models;
  7. namespace CascadingDropDownInMVC4.Controllers
  8. {
  9. public class EmployeeController : Controller
  10. {
  11. public ActionResult Index()
  12. {
  13. EmployeeManagementEntities db = new EmployeeManagementEntities();
  14. ViewBag.Country = new SelectList(db.Country, "CountryID", "CountryName");
  15. return View();
  16. }
  17. // Json Call to get state
  18. public JsonResult GetStates(string id)
  19. {
  20. List<SelectListItem> states = new List<SelectListItem>();
  21. var stateList = this.Getstate(Convert.ToInt32(id));
  22. var stateData = stateList.Select(m => new SelectListItem()
  23. {
  24. Text = m.StateName,
  25. Value = m.StateID.ToString(),
  26. });
  27. return Json(stateData, JsonRequestBehavior.AllowGet);
  28. }
  29. // Get State from DB by country ID
  30. public IList<State> Getstate(int CountryId)
  31. {
  32. EmployeeManagementEntities db = new EmployeeManagementEntities();
  33. return db.State.Where(m => m.CountryID == CountryId).ToList();
  34. }
  35. [HttpPost]
  36. public ActionResult Index(FormCollection formdata)
  37. {
  38. // Get Employee informaiton to insert into Data Base
  39. return RedirectToAction("Index", "Home");
  40. }
  41. }
  42. }
Now add a View index.cshtml as in the following:
  1. @model CascadingDropDownInMVC4.Models.EmployeeData
  2. @{
  3. ViewBag.Title = "Index";
  4. }
  5. <h2>Index</h2>
  6. @using (Html.BeginForm())
  7. {
  8. @Html.ValidationSummary(true)
  9. <fieldset>
  10. <legend>EmployeeData</legend>
  11. <div class="editor-label">
  12. @Html.LabelFor(model => model.Name)
  13. </div>
  14. <div class="editor-field">
  15. @Html.EditorFor(model => model.Name)
  16. @Html.ValidationMessageFor(model => model.Name)
  17. </div>
  18. <div class="editor-label">
  19. @Html.LabelFor(model => model.Email)
  20. </div>
  21. <div class="editor-field">
  22. @Html.EditorFor(model => model.Email)
  23. @Html.ValidationMessageFor(model => model.Email)
  24. </div>
  25. <div class="editor-label">
  26. @Html.LabelFor(model => model.Designation)
  27. </div>
  28. <div class="editor-field">
  29. @Html.EditorFor(model => model.Designation)
  30. @Html.ValidationMessageFor(model => model.Designation)
  31. </div>
  32. <div class="editor-label">
  33. @Html.Label("Country")<br />
  34. </div>
  35. <div>
  36. @Html.DropDownList("Country", ViewBag.Country as SelectList, "-- Please Select a Country --", new { style="width:250px"})
  37. </div>
  38. <div class="editor-label"><br />
  39. @Html.Label("State")<br />
  40. </div>
  41. <div>
  42. @Html.DropDownList("State", new SelectList(string.Empty, "Value", "Text"), "-- Please select a State --", new { style = "width:250px", @class = "dropdown1" })
  43. </div>
  44. <p>
  45. <input type="submit" value="Create" />
  46. </p>
  47. </fieldset>
  48. }
  49. <script src="~/Scripts/jquery-1.7.1.js"></script>
  50. <script src="~/Scripts/jquery-1.7.1.min.js"></script>
  51. <script type="text/javascript">
  52. $(document).ready(function () {
  53. //Country Dropdown Selectedchange event
  54. $("#Country").change(function () {
  55. $("#State").empty();
  56. $.ajax({
  57. type: 'POST',
  58. url: '@Url.Action("GetStates")', // Calling json method
  59. dataType: 'json',
  60. data: { id: $("#Country").val() },
  61. // Get Selected Country ID.
  62. success: function (states) {
  63. $.each(states, function (i, state) {
  64. $("#State").append('<option value="' + state.Value + '">' +
  65. state.Text + '</option>');
  66. });
  67. },
  68. error: function (ex) {
  69. alert('Failed to retrieve states.' + ex);
  70. }
  71. });
  72. return false;
  73. })
  74. });
  75. </script>
Now run the application as in the following:

Now select a country as in the following:





Now I have 2 tables for Country & State.



The following is the data in my Country table:

The following is the data in my State table: