Procedure

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

Now add a AngularJS reference. Right-click on the project in the Solution Explorer and select Manage NuGet Packages.







Now add an ADO.NET Entity Data Model.













Now add a new Controller Employee and enter the following code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using CascadingDropDownInMVC4WithAngularJS.Models;
  7. namespace CascadingDropDownInMVC4WithAngularJS.Controllers
  8. {
  9. public class EmployeeController : Controller
  10. {
  11. EmployeeManagementEntities db=new EmployeeManagementEntities();
  12. public ActionResult Index()
  13. {
  14. return View();
  15. }
  16. public JsonResult GetCountry()
  17. {
  18. var CountryList = db.Country.ToList();
  19. return this.Json(CountryList, JsonRequestBehavior.AllowGet);
  20. }
  21. [HttpPost]
  22. public JsonResult GetStates(int CountryID)
  23. {
  24. var StateList = db.State.Where(m => m.CountryID == CountryID).ToList();
  25. return this.Json(StateList);
  26. }
  27. }
  28. }

Now add a View on the Index Action method:

  1. @Scripts.Render("~/Scripts/angular.min.js")
  2. <script type="text/javascript">
  3. //Module
  4. var myApp = angular.module('myApp', []);
  5. //Controller
  6. myApp.controller('MainCtrl', ['$scope', '$http',
  7. function ($scope, $http) {
  8. //$http service for Getting the Country
  9. $http({
  10. method: 'GET',
  11. url: '/Employee/GetCountry'
  12. }).
  13. success(function (data) {
  14. $scope.country = data;
  15. });
  16. //$http service for getting States
  17. $scope.GetStates = function () {
  18. if ($scope.countr) {
  19. $http({
  20. method: 'POST',
  21. url: '/Employee/GetStates/',
  22. data: JSON.stringify({ CountryID: $scope.countr })
  23. }).
  24. success(function (data) {
  25. $scope.states = data;
  26. });
  27. }
  28. else {
  29. $scope.cities = null;
  30. }
  31. }
  32. }]);
  33. </script>
  34. <div data-ng-app="myApp">
  35. <div data-ng-controller="MainCtrl">
  36. <div class="editor-label">
  37. <label>Name</label>
  38. </div>
  39. <div class="editor-field">
  40. @Html.TextBox("Name")
  41. </div>
  42. <div class="editor-label">
  43. <label>Country</label><br />
  44. </div>
  45. <div class="editor-field">
  46. <select data-ng-model="countr"
  47. data-ng-options="c.CountryID as c.CountryName for c in country"
  48. data-ng-change="GetStates()">
  49. <option value="">--Select Country--</option>
  50. </select><br />
  51. </div>
  52. <div class="editor-label"><br />
  53. <label>State</label><br />
  54. </div>
  55. <div class="editor-field">
  56. <select data-ng-model="state" data-ng-disabled="!states"
  57. data-ng-options="s.StateID as s.StateName for s in states">
  58. <option value="">--Select State--</option>
  59. </select>
  60. </div>
  61. </div>
  62. </div>

Now run you application: