I want to filter state province drop-down based on country in admin [custom work]. I added in product controller action method that returns list of states as Json result. but when I serialize the list it gives me error list reference loop
Below is the method
[HttpGet]
public ActionResult getAreasByCityId(int cityId)
{
var areas = _stateProvinceService.GetStateProvincesByCountryId(cityId);
if (areas.Any())
{
var json = JsonConvert.SerializeObject(areas.ToList(), Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
});
return Json(new
{
success = true,
Data = json,
}, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new
{
success = false,
message = _localizationService.GetResource("nodata")
}, JsonRequestBehavior.AllowGet);
}
}
And below is the calling function from the view:
<script>
$(document).ready(function() {
$('#CountryId').on('change', function (e) {
getAreasByCityId();
});
function getAreasByCityId(){
var cityId = $('#CountryId').val();
var url = "/Admin/Product/getAreasByCityId?cityId=" + cityId;
$.ajax({
cache: false,
url: url,
type: 'Get',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
beforeSend: function () {
$('#ajaxBusy').show();
},
success: function (data) {
if (data != null) {
$.each(data,function(i,obj)
{
});
}
},
complete: function (data) {
$('#ajaxBusy').hide();
},
error: function (error) {
//alert(error.responseText);
}
});
}
});
</script>
Any suggestions?