Populate Cascading Dropdown Using Web API and AngularJS

Basically, whenever we want to create a Cascading Dropdown List, it basically populates another Dropdown list on the basis of the selected item of the first Dropdown list. In this article, we basically create two Dropdown lists, one for Department List and another for the Designation List. Whenever we select a department name from the list, it populates the designation list on the basis of the selected department code.
 
To implement this, we will start by creating an ASP.NET MVC Application. For these, you can press CTRL + SHIFT + N or you can click on "File" -> "New" -> "Project...". Then select ASP.Net Web Application and provide the project a name and click the OK button.

 
Now, in the next tab, select MVC application and below that check the MVC and Web API Check boxes and click on the OK button.
 
 
This will create a starter web application.
 
Now, we need to create two model classes named Departrment and Designation. For this, select the Model folder and right-click and select Add Class. Now add the following model class:
  1. public class Department    
  2. {    
  3.     public int DeptID { getset; }    
  4.     public string DeptName { getset; }    
  5. }    
  6.   
  7. public class Designation    
  8. {    
  9.     public int DesigID { getset; }    
  10.     public string DesigName { getset; }    
  11.     public int DeptID { getset; }    
  12. }  
Now, we need to add a Web API Controller. For this, Select Controller folder and right-click and click Add Controller.
 
 
 
Type the Controller Name as DeptListController and provide the following code there.
  1. public List<Department> Get()    
  2. {    
  3.     List<Department> lstDept = GetDeptList();    
  4.     return lstDept;    
  5. }    
  6.   
  7. private List<Department> GetDeptList()    
  8. {    
  9.     List<Department> lstData = new List<Department>();    
  10.   
  11.     Department objDept = new Department();    
  12.     objDept.DeptID = 0;    
  13.     objDept.DeptName = "";    
  14.     lstData.Add(objDept);    
  15.   
  16.     objDept = new Department();    
  17.     objDept.DeptID = 1;    
  18.     objDept.DeptName = "Sales";    
  19.     lstData.Add(objDept);    
  20.   
  21.     objDept = new Department();    
  22.     objDept.DeptID = 2;    
  23.     objDept.DeptName = "Accounts";    
  24.     lstData.Add(objDept);    
  25.   
  26.     objDept = new Department();    
  27.     objDept.DeptID = 3;    
  28.     objDept.DeptName = "Marketing";    
  29.     lstData.Add(objDept);    
  30.   
  31.     objDept = new Department();    
  32.     objDept.DeptID = 4;    
  33.     objDept.DeptName = "Administrator";    
  34.     lstData.Add(objDept);    
  35.   
  36.     objDept = new Department();    
  37.     objDept.DeptID = 5;    
  38.     objDept.DeptName = "Purchase";    
  39.     lstData.Add(objDept);    
  40.     return lstData;    

Similarly add another Web API Controller with the name DesigListController and provide the following code.
  1. public List<Designation> Post(Department objData)    
  2. {    
  3.     List<Designation> lstDesig = new List<Designation>();    
  4.   
  5.     if (objData.DeptID > 0)    
  6.         lstDesig = GetDesigList().Where(s => s.DeptID.Equals(objData.DeptID)).ToList();    
  7.     return lstDesig;    
  8. }    
  9.   
  10. private List<Designation> GetDesigList()    
  11. {    
  12.     List<Designation> lstData = new List<Designation>();    
  13.   
  14.     Designation objDesig = new Designation();    
  15.     objDesig.DesigID = 1;    
  16.     objDesig.DesigName = "Sales Manager";    
  17.     objDesig.DeptID = 1;    
  18.     lstData.Add(objDesig);    
  19.   
  20.     objDesig = new Designation();    
  21.     objDesig.DesigID = 2;    
  22.     objDesig.DesigName = "Sr. Sales Manager";    
  23.     objDesig.DeptID = 1;    
  24.     lstData.Add(objDesig);    
  25.   
  26.     objDesig = new Designation();    
  27.     objDesig.DesigID = 3;    
  28.     objDesig.DesigName = "Accountant";    
  29.     objDesig.DeptID = 2;    
  30.     lstData.Add(objDesig);    
  31.   
  32.     objDesig = new Designation();    
  33.     objDesig.DesigID = 4;    
  34.     objDesig.DesigName = "Auditor";    
  35.     objDesig.DeptID = 2;    
  36.     lstData.Add(objDesig);    
  37.   
  38.     objDesig = new Designation();    
  39.     objDesig.DesigID = 5;    
  40.     objDesig.DesigName = "Agent";    
  41.     objDesig.DeptID = 3;    
  42.     lstData.Add(objDesig);    
  43.   
  44.     objDesig = new Designation();    
  45.     objDesig.DesigID = 6;    
  46.     objDesig.DesigName = "Marketing Manager";    
  47.     objDesig.DeptID = 3;    
  48.     lstData.Add(objDesig);    
  49.   
  50.     objDesig = new Designation();    
  51.     objDesig.DesigID = 7;    
  52.     objDesig.DesigName = "Sr. Sales Manager";    
  53.     objDesig.DeptID = 1;    
  54.     lstData.Add(objDesig);    
  55.   
  56.     objDesig = new Designation();    
  57.     objDesig.DesigID = 8;    
  58.     objDesig.DesigName = "Network Administrator";    
  59.     objDesig.DeptID = 4;    
  60.     lstData.Add(objDesig);    
  61.   
  62.     objDesig = new Designation();    
  63.     objDesig.DesigID = 9;    
  64.     objDesig.DesigName = "Purchase Office";    
  65.     objDesig.DeptID = 6;    
  66.     lstData.Add(objDesig);    
  67.   
  68.     objDesig = new Designation();    
  69.     objDesig.DesigID = 10;    
  70.     objDesig.DesigName = "Supervisor";    
  71.     objDesig.DeptID = 6;    
  72.     lstData.Add(objDesig);    
  73.     return lstData;    
  74. }  
Basically, this web API controller is called when we select a department from the dropdown list. It takes the department model class as argument to provide information about the selected department and fetches the designation list as in the selected deparment.
 
Now, we need to install the Angular JS Reference using Nuget Console application. After the installation is done, the Angular js related script files has been added under the script tag.
 
Now, create a new folder named Html under the Root of the Main project. Now add 2 blank HTML files within that  Html Folder with the name MainPage.Html and Dropdown.html.
 
Now add the following code to the MainPage.Html: 
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.   
  6.     <script src="../../Scripts/angular1.3.8.min.js"></script>  
  7.     <script>  
  8.         var myApp = angular.module('MyApp', [])  
  9.         .controller("MyController", ['$scope''$http',  
  10.             function ($scope, $http) {  
  11.                $http.get("/api/DeptList").then(function (responses) {  
  12.                     $scope.DeptMas = responses.data;  
  13.                     $scope.Department = $scope.DeptMas[0];  
  14.                 });  
  15.   
  16.                $scope.OnDeptChange = function () {  
  17.                    $http.post("/api/DesigList", $scope.Department).then(function (responses) { 
  18.                        $scope.DesigMas = responses.data;  
  19.                        $scope.Designation = $scope.DesigMas[0];  
  20.                     });  
  21.                 }  
  22.             }]);  
  23.   
  24.         myApp.directive("ngDropdown", function () {  
  25.             return {  
  26.                 restrict: "E",  
  27.                 templateUrl: "DownDown.html"  
  28.             }  
  29.         });         
  30.     </script>  
  31. </head>  
  32. <body ng-app="MyApp">  
  33.    <ng-dropdown></ng-dropdown>      
  34. </body>  
  35. </html>  
Now add the following code to the DropDown.Html:
  1. <div ng-controller="MyController">  
  2.     <table>  
  3.         <tr>  
  4.             <td>Select Department Name :</td>  
  5.             <td> <select ng-options="a.DeptName for a in DeptMas" ng-model="Department" ng-change="OnDeptChange()" name="ddlDept"></select> </td>  
  6.         </tr>  
  7.         <tr>  
  8.             <td>Select Designation Name :</td>  
  9.             <td> <select ng-options="a.DesigName for a in DesigMas" ng-model="Designation" name="ddlDesig"></select></td>  
  10.         </tr>  
  11.     </table>      
  12. </div>  
Now, as in the preceding HTML code, we create an Angular JS Controller named MyController within MainPage.html and call that controller from the dropdown.html page for data binding. Also, we create another custom directive named ngDropdown that basically calls the Dropdown controls within the main page. Basically it works just like the user controls concept in traditional web forms project or partial views concept in MVC. 


Similar Articles