0

I create controller function getting data as json. And in view I used this function to get data in ajax request. When I give model in return block

return Json(model, JsonRequestBehavior.AllowGet); 

like this I got this error

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

when I give json in return block like this

return Json(json, JsonRequestBehavior.AllowGet); 

then it gives double serialization problem, I could not handle it

controller function

[HttpPost]
    public JsonResult GetallData(int id)
    {
        MultipleViewModel model = new MultipleViewModel();
        List<InformationSecurityData> iso = db.InformationSecurityDatas.Where(x => x.Supplier_ID == id).ToList();
        List<SurveyForm> tender = db.SurveyForms.Where(x => x.Supplier_ID == id ).ToList();
        List<ProcurementData> procurement = db.ProcurementDatas.Where(x => x.Supplier_ID == id ).ToList();
        List<Supplier> supp = db.Suppliers.Where(x => x.Supplier_ID == id).ToList();

        model.Inf_Sec_Dat = iso;
        model.Proc_Dat = procurement;
        model.Tender_Dat = tender;
        model.Supp_dat = supp;

        var serializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };
        string json = JsonConvert.SerializeObject(model, Formatting.Indented, serializerSettings);
        MultipleViewModel model2 = JsonConvert.DeserializeObject<MultipleViewModel>(json);

        return Json(model, JsonRequestBehavior.AllowGet);
    }

view

$(document).ready(function () {
            $("#GetallData").click(function () {
                var id = $("#id").val();
                $.ajax({
                    url: '/Dashboard/GetallData/'+id,
                    type: 'POST',
                    cache: false,
                    dataType: 'json',
                    contentType: 'application/json',
                    success: function (data) {
                        alert("success ");
                        //var myJSON = JSON.stringify(obj);
                        Objdata = jQuery.parseJSON(data);
                        alert(Objdata);
                        if (Objdata.Inf_Sec_Dat) {
                            $.each(Objdata.Inf_Sec_Dat, function (i, item) {
                                iso_data.push(item.Total_Score);
                            });
                            alert(iso_data);
                        }
                        bindChart(iso_data);
                        if (Objdata.Tender_Dat) {
                            $.each(Objdata.Tender_Dat, function (i, item) {
                                tender_data.push(item.Total_Score);
                            });
                        }
                        bindChart2(tender_data);
                        //bindChart3(tender_data);
                    },
                    error: function (response) {
                        alert("error_tender" + response);
                    }
                });
            });
        });

please help I could not handle

kyr
  • 1
  • 2
  • this is a duplicate of [Circular reference detected exception while serializing object to JSON](https://stackoverflow.com/a/16954302/6794089). This answer outlines how to prevent EF generated proxy classes from being serialized. – bman7716 May 15 '18 at 19:35
  • so what should I do – kyr May 15 '18 at 19:40
  • 1
    The ticket that I linked to in my original comment has several solutions you can use. 1. Disable EF's proxy creation (but you lose lazy loading in the process), 2. Tell your JSON serializer to not to serialize proxy classes. 3. When composing your EF query specify `.AsNoTracking()` (this is what I would recommend since you aren't performing any updates or inserts your `GetallData` action). Please refer to the the answers linked in my original comment for examples of how to do each option. – bman7716 May 15 '18 at 19:54
  • Another commonly used pattern is to return a DTO object (not linked to entity framework) which just contains the fields you actually need in the client-side. You'd have to write a small amount of code to create that object and copy the specific properties from your EF objects to the DTO, and then return it (or a list of it, if necessary). Tools like AutoMapper can help to automate that – ADyson May 15 '18 at 20:10
  • I examined this solutions but when I add this serializer then my ajax data is double serialized so I could not use data in ajax – kyr May 15 '18 at 20:12
  • What about the `AsNoTracking()` on your EF query? Example: `List iso = db.InformationSecurityDatas.AsNoTracking().Where(x => x.Supplier_ID == id).ToList();`. Add the `.AsNoTracking()` to every database query. Does this provide different results? – bman7716 May 15 '18 at 21:47
  • I add thank you it works how can I vote your comment – kyr May 15 '18 at 22:12
  • Where did anyone suggest double serialising your data? – ADyson May 16 '18 at 05:19

0 Answers0