Populate a DropDownList in Change of Another DropDownList

In this example, we populate two DropDownLists. One for Department List and another for Designation List. The Department list is first executed. Then, on the basis of the selected department name, the designation list is populated.
For this, first we need to create two model classes for Department and Designation as below:
  1. public class DeptMas  
  2.     {  
  3.         public int DEPTCODE { get; set; }  
  4.         public string DEPTDESC { get; set; }          
  5.     }  
  1. public class DesigMas  
  2.     {  
  3.         public int DESIGCODE { get; set; }  
  4.          public string DESIGDESC { get; set; }  
  5.         public int DEPTCODE { get; set; }         
  6.     }  
Now to create the Controller. For that we first create the following method to populate the department list: 
  1.          public ActionResult GetDept()  
  2.         {  
  3.             List<DeptMas> lstDept = new List<DeptMas>();  
  4.             DeptMas objDept;  
  5.             objDept = new DeptMas();  
  6.             objDept.DEPTCODE = 1;  
  7.             objDept.DEPTDESC = "Accounts";  
  8.             lstDept.Add(objDept);  
  9.             objDept = new DeptMas();  
  10.             objDept.DEPTCODE = 2;  
  11.             objDept.DEPTDESC = "Administrator";  
  12.             lstDept.Add(objDept);  
  13.             objDept = new DeptMas();  
  14.             objDept.DEPTCODE = 3;  
  15.             objDept.DEPTDESC = "Sales";  
  16.             lstDept.Add(objDept);  
  17.             objDept = new DeptMas();  
  18.             objDept.DEPTCODE = 4;  
  19.             objDept.DEPTDESC = "Marketing";  
  20.             lstDept.Add(objDept);  
  21.             return Json(lstDept, JsonRequestBehavior.AllowGet);  
  22.         }  
Now we want to write the method to populate the designation. This method will take DeptCode as argument and provide only those designation lists tagged with the given department.
 
  1. public ActionResult GetDesig(int DeptCode)  
  2.         {  
  3.             List<DesigMas> lstDesig = PopulateDesignation().Where(s => s.DEPTCODE.Equals(DeptCode)).ToList();  
  4.             return Json(lstDesig, JsonRequestBehavior.AllowGet);  
  5.         }  
  6.   
  7.         public List<DesigMas> PopulateDesignation()  
  8.         {  
  9.             List<DesigMas> lstDesig = new List<DesigMas>();  
  10.             DesigMas objDesig;  
  11.             objDesig = new DesigMas();  
  12.             objDesig.DESIGCODE = 1;  
  13.             objDesig.DESIGDESC = "Accountants";  
  14.             objDesig.DEPTCODE = 1;  
  15.             lstDesig.Add(objDesig);  
  16.   
  17.             objDesig = new DesigMas();  
  18.             objDesig.DESIGCODE = 2;  
  19.             objDesig.DESIGDESC = "Sr Accountant";  
  20.             objDesig.DEPTCODE = 1;  
  21.             lstDesig.Add(objDesig);  
  22.   
  23.             objDesig = new DesigMas();  
  24.             objDesig.DESIGCODE = 3;  
  25.             objDesig.DESIGDESC = "Sales Man";  
  26.             objDesig.DEPTCODE = 3;  
  27.             lstDesig.Add(objDesig);  
  28.   
  29.             objDesig = new DesigMas();  
  30.             objDesig.DESIGCODE = 4;  
  31.             objDesig.DESIGDESC = "Manager";  
  32.             objDesig.DEPTCODE = 4;  
  33.             lstDesig.Add(objDesig);  
  34.   
  35.             objDesig = new DesigMas();  
  36.             objDesig.DESIGCODE = 5;  
  37.             objDesig.DESIGDESC = "Agent";  
  38.             objDesig.DEPTCODE = 4;  
  39.             lstDesig.Add(objDesig);  
  40.   
  41.             return lstDesig;  
  42.         }  
Now to design the view. For the view, please use the following code: 
  1. @{  
  2.     Layout = "~/Views/Shared/_Layout.cshtml";  
  3. }  
  4. <script type="text/javascript">  
  5.     $(document).ready(function () {  
  6.         $.ajax({  
  7.             url: '@Url.Action("GetDept", "Employee")',  
  8.             type: "GET",  
  9.             success: function (json, textStatus) {  
  10.                 json = json || {};  
  11.                 for (var i = 0; i < json.length; i++) {  
  12.                     $("#ddlDept").append('<option value="' + json[i].DEPTCODE + '">' + json[i].DEPTDESC + '</option>');  
  13.                 }  
  14.             },  
  15.             error: function () {  
  16.                 alert("Data Not Found");  
  17.             }  
  18.         });  
  19.     });  
  20.   
  21.     $(document).ready(function () {  
  22.         $("#ddlDesig").prop("disabled"true);  
  23.         $("#ddlDept").change(function () {  
  24.             var id = $("#ddlDept").val();  
  25.             $.ajax({  
  26.                 cache: false,  
  27.                 type: "GET",  
  28.                 url: '@Url.Action("GetDesig", "Employee")',  
  29.                 data: { "DeptCode": id },  
  30.                 success: function (json, textStatus) {  
  31.                     $("#ddlDesig").empty();  
  32.                     json = json || {};  
  33.                     for (var i = 0; i < json.length; i++) {  
  34.                         $("#ddlDesig").append('<option value="' + json[i].DESIGCODE + '">' + json[i].DESIGDESC + '</option>');  
  35.                     }  
  36.                     $("#ddlDesig").prop("disabled"false);  
  37.                 },  
  38.                 error: function () {  
  39.                     alert("Data Not Found");  
  40.                 }  
  41.         });  
  42.         });  
  43.     });  
  44.   
  45.     function fnSubmit() {  
  46.         var msg = "Dept Code : " + $("#ddlDept").val() + " Dept Name : " + $("#ddlDept option:selected").text() + "</br>";  
  47.         msg += "Desig Code : " + $("#ddlDesig").val() + " Desig Name : " + $("#ddlDesig option:selected").text();  
  48.         alert(msg);  
  49.     }  
  50. </script>  
  51. <h4>Drop Down Control</h4>  
  52. <table>  
  53.     <tr>  
  54.         <td style="width:15%;" class="alignRight">  
  55.             <span class="menuItemBold">Select Department</span>  
  56.         </td>  
  57.         <td style="width:35%;">  
  58.             <select id="ddlDept"></select>  
  59.         </td>  
  60.         <td style="width:15%;" class="alignRight">  
  61.             <span class="menuItemBold">Select Designation</span>  
  62.         </td>  
  63.         <td style="width:35%;">  
  64.             <select id="ddlDesig"></select>  
  65.         </td>  
  66.     </tr>  
  67.     <tr>  
  68.         <td>  
  69.             <input type="submit" id="btnSubmit" value="Submit"  onclick="fnSubmit();"/>  
  70.         </td>  
  71.     </tr>  
  72. </table>  

Up Next
    Ebook Download
    View all
    Learn
    View all