Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Unable to bind Cascading Dropdowns - jQuery AJAX

What I have tried:

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
C#
[HttpGet]  
       public JsonResult GetDivisionsByRegions(string regionID = "")  
       {  
           try  
           {  
               List<Division> allDivisions = new List<Division>();  
               int ID = 0;  
               if (int.TryParse(regionID, out ID))  
               {  
                   using (GeoEntities data = new GeoEntities())  
                   {  
                       allDivisions = data.Divisions.Where(div => div.RegionID == ID).OrderBy(div => div.DivisionID).ToList();  
                   }  
               }  
               if (Request.IsAjaxRequest())  
               {  
                   return new JsonResult  
                   {  
                       Data = allDivisions,  
                       JsonRequestBehavior = JsonRequestBehavior.AllowGet  
                   };  
               }  
               else  
               {  
                   return new JsonResult  
                   {  
                       Data = "Invalid Data",  
                       JsonRequestBehavior = JsonRequestBehavior.AllowGet  
                   };  
               }  
           }  
           finally  
           {  
           }  
       }  

jQuery AJAX call to GET data from & bind to Dropdown.
JavaScript
<script type="text/javascript">  
    $(document).ready(function () {  
        $('#RegionID').change(function () {  
            var regionID = parseInt($('#RegionID').val());  
            if (!isNaN(regionID)) {  
                var ddDivision = $('#DivisionID');  
                ddDivision.empty();  
                ddDivision.append($("<option></option>").val("").html("Select Division"));  
                $.ajax({  
                    url: "@Url.Action("GetDivisionsByRegions","GetGeoData")",  
                    type: "GET",  
                    data: { regionID: regionID },  
                    dataType: "json",  
                    success: function (data) {  
                        $.each(data, function (i, value) {  
                            ddDivision.append(  
                                $('<option></option>').val(value.DivisionID).html(value.DivisionName)  
                                );  
                        });  
                    },  
                    error: function () {  
                        alert('Sorry!! Error');  
                    }  
  
                });  
              }  
        });  
    });  
</script>

Able to get data from entity.
Error when binding data to Division drop down.(alert shows 'Sorry!! Error').

Please help me out.
Posted
Updated 8-Apr-17 21:06pm
Comments
Karthik_Mahalingam 9-Apr-17 2:17am    
Check in chrome console for detailed error
CuriousToLearn 12-Apr-17 12:55pm    
Thanks Karthik, for your response. I have already tried looking into browser console but i didn't see any log.
CuriousToLearn 13-Apr-17 12:15pm    
Hi Karthik, thanks again.
I have encountered following two exceptions:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies'

And
context.Configuration.ProxyCreationEnabled = false;
solved the problem.

1 solution

$(document).ready(function () {
$('#RegionID').change(function () {
var regionID = parseInt($('#RegionID').val());
if (!isNaN(regionID)) {
var ddDivision = $('#DivisionID');
ddDivision.empty();
ddDivision.append($("").val("").html("Select Division"));
$.ajax({
url: "@Url.Action("GetDivisionsByRegions","GetGeoData")",
type: "GET",
data: { regionID: regionID },
dataType: "json",
success: function (data) {
$.each(data.d, function (i, value) {
ddDivision.append(
$('').val(value.DivisionID).html(value.DivisionName)
);
});
},
error: function () {
alert('Sorry!! Error');
}

});
}
});
});------------
Check in browser console, The returned object may be in the data.d.
So try data.d in
$.each(data.d, function (i, value) ......
 
Share this answer
 
v2
Comments
CuriousToLearn 12-Apr-17 12:50pm    
Thanks Ramesh Kumar Barik, for your reply.
I tried $.each(data.d, function (i, value){...}) but still problem persists.
CuriousToLearn 13-Apr-17 12:16pm    
Hi Ramesh Kumar Barik, thanks again.
I have encountered following two exceptions:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies'

And
DataEntities data = new DataEntities();
data.Configuration.ProxyCreationEnabled = false;

solved the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900