Error : jsonconvert.serializeobject list reference loop

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hi,

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?
6 years ago
Hi,

You need to create you own Class object and than assign all value to that new class.

Because into this method GetStateProvincesByCountryId return parameter has call another class and that class also call GetStateProvincesByCountryId's class due to that reason you get error.

So that you need to create one model class object and assign all required value and than call your json like below.


var jsonString = JsonConvert.SerializeObject(
                areas, //here your class object name.
                Formatting.None,
                new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                });
  

Also refer this link.

Let me know if you want any further help.

Thanks,
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.