L A

L A

  • NA
  • 170
  • 171.2k

Unable to bind Cascading Dropdowns - jQuery AJAX

Apr 9 2017 12:47 AM
My DB consists of Regions, Divisions & States. My application (ASP.NET MVC) application uses entityframework to bind data to DropDown.
  • Created Action to bind Regions by default.
  • Created Action with RegionId as parameter to retrive Divisions
  1. [HttpGet]  
  2.        public JsonResult GetDivisionsByRegions(string regionID = "")  
  3.        {  
  4.            try  
  5.            {  
  6.                List<Division> allDivisions = new List<Division>();  
  7.                int ID = 0;  
  8.                if (int.TryParse(regionID, out ID))  
  9.                {  
  10.                    using (GeoEntities data = new GeoEntities())  
  11.                    {  
  12.                        allDivisions = data.Divisions.Where(div => div.RegionID == ID).OrderBy(div => div.DivisionID).ToList();  
  13.                    }  
  14.                }  
  15.                if (Request.IsAjaxRequest())  
  16.                {  
  17.                    return new JsonResult  
  18.                    {  
  19.                        Data = allDivisions,  
  20.                        JsonRequestBehavior = JsonRequestBehavior.AllowGet  
  21.                    };  
  22.                }  
  23.                else  
  24.                {  
  25.                    return new JsonResult  
  26.                    {  
  27.                        Data = "Invalid Data",  
  28.                        JsonRequestBehavior = JsonRequestBehavior.AllowGet  
  29.                    };  
  30.                }  
  31.            }  
  32.            finally  
  33.            {  
  34.            }  
  35.        } 
  • jQuery AJAX call to GET data from & bind to Dropdown.
  1. <script type="text/javascript">  
  2.     $(document).ready(function () {  
  3.         $('#RegionID').change(function () {  
  4.             var regionID = parseInt($('#RegionID').val());  
  5.             if (!isNaN(regionID)) {  
  6.                 var ddDivision = $('#DivisionID');  
  7.                 ddDivision.empty();  
  8.                 ddDivision.append($("<option></option>").val("").html("Select Division"));  
  9.                 $.ajax({  
  10.                     url: "@Url.Action("GetDivisionsByRegions","GetGeoData")",  
  11.                     type: "GET",  
  12.                     data: { regionID: regionID },  
  13.                     dataType: "json",  
  14.                     success: function (data) {  
  15.                         $.each(data, function (i, value) {  
  16.                             ddDivision.append(  
  17.                                 $('<option></option>').val(value.DivisionID).html(value.DivisionName)  
  18.                                 );  
  19.                         });  
  20.                     },  
  21.                     error: function () {  
  22.                         alert('Sorry!! Error');  
  23.                     }  
  24.   
  25.                 });  
  26.   
  27.             }  
  28.         });  
  29.     });  
  30. </script> 
  • Able to get data from entity.


  • Error when bindig to Division drop down


Please help me out..

Answers (1)