簡體   English   中英

序列化一個類型的對象時檢測到循環引用

[英]Circular reference was detected while serializing an object of type

我正在嘗試通過ajax獲取基於公司ID的公司數據,並使用接收到的數據填充相關的文本框。 通過ajax自動完成功能選擇公司ID。 兩天后,代碼運行良好,突然,它僅針對前兩個自動完成條目開始生成錯誤,而對於其余部分,則運行正常。 誰能指出這個錯誤。 謝謝。

錯誤詳細信息: “消息”:“序列化類型為\\ u0027System.Data.Entity.DynamicProxies.Company_81625299B5B4D7A3375D55E48BE84921728B8D48335366DF8CA6844A8D10FF5D \\ u0027。”,“ StackTrace”:“在Systemization.WebScript上,檢測到循環引用。 SerializeValueInternal(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\\ r \\ n。

下面是我的代碼:

function setCompanyData(pageurl, txtboxid, txtResultid) {
var temp = true;
var searchTbox = $jq14("[id$=" + txtboxid + "]");
var resultTbox = $jq14("[id$=" + txtResultid + "]");

searchTbox.autocomplete({
    source: function (request, response) {
        $jq14.ajax({
            url: pageurl,
            data: "{ 'SearchText': '" + request.term + "'}",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                response($.map(data.d, function (item) {

                    return {
                        label: item.split('*')[0],
                        val: item.split('*')[1]
                    }
                }))
            },
            error: function (response) {
                alert(response.responseText);
            },
            failure: function (response) {
                alert(response.responseText);
            }
        });
    },
    autoSelect: true,
    autoFocus: true,
    select: function (e, i) {
        // e.preventDefault();
        searchTbox.val(i.item.label);
        resultTbox.val(i.item.val).trigger('change');
        temp = true;
        // return false;
    },
    change: function (e, i) {

        var cId = resultTbox.val();
        if (isEmptyOrSpaces(cId)) {
            // error 
            cId = 0;
            searchTbox.val("").trigger('');
        }
        if (isEmptyOrSpaces(searchTbox.val())) {
            // error 
            cId = 0;
            resultTbox.val("").trigger('change');
        }
        getCompanyDetails(cId);
    },
    minLength: 0
}).focus(function () {
    if (temp) {
        $jq14(this).autocomplete("search", "");
        temp = false;
    }
});

searchTbox.autocomplete("widget").addClass("fixedHeight");}


function getCompanyDetails(cid) {
 $jq14.ajax({
     url: "/sw/adm/update-delete-company.aspx/GetCompanyData",
     data: "{ 'cId': '" + cid + "'}",
     dataType: "json",
     type: "POST",
     contentType: "application/json; charset=utf-8",
     beforeSend: function () {
         $('#loader').show();
     },
     complete: function () {
         $('#loader').hide();
         $("#messageBox").hide().slideDown();
         setTimeout(function () {
             $("#messageBox").fadeOut(); 
         }, 5000);

     },
     success: function (result) {
         $jq14('#cphMainLeft_txtAddress').val(result.d.CompanyAddress);
         $jq14('#cphMainLeft_txtCountry').val(result.d.CompanyCountry);
         $jq14('#cphMainLeft_txtCity').val(result.d.CompanyCity);
         $jq14('#cphMainLeft_txtNewCompanyName').val(result.d.CompanyName);
         $jq14('#cphMainLeft_txtEmail').val(result.d.CompanyEmail);
         $jq14('#cphMainLeft_txtPanNo').val(result.d.CompanyPAN);
         $jq14('#cphMainLeft_txtTinNo').val(result.d.CompanyTIN);
         $jq14('#cphMainLeft_txtPhone').val(result.d.CompanyPhone);
         $jq14('#cphMainLeft_txtPincode').val(result.d.CompanyPincode);
         $jq14('#cphMainLeft_hfTxtCountry').val(result.d.CompanyCountry);
         $jq14("#cphMainLeft_ddlCompanyType").val(result.d.CompanyType);
     },
     error: function (response) {
         alert(response.responseText);
     },
     failure: function (response) {
         alert(response.responseText);
     }
 });
}

C#Webmethod類似於:

    [WebMethod]
    public static Company GetCompanyData(int cId)
    {
        Entities db = new Entities();

        var companyRecord = (from cmp in db.Companies
                                 where cmp.CompanyId == cId
                                 select cmp).SingleOrDefault();

            if (companyRecord != null)
                return companyRecord;
            else
                return new Company();
    }

謝謝Ashokkumar M. Prajapati提供的提示。

我沒有從[WebMethod]返回Company對象,而是在后面的代碼中將company對象轉換為Json字符串並返回了。

這是我的WebMethod:

[WebMethod]
public static string GetCompanyData(int cId)
{
    Entities db = new Entities();

    var companyRecord = (from cmp in db.Companies
                             where cmp.CompanyId == cId
                             select cmp).SingleOrDefault();


        if (companyRecord == null)
            companyRecord = new Company();

        string s = string.Empty;
        s = JsonConvert.SerializeObject(companyRecord,
                       new JsonSerializerSettings
                       {
                           ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                       });
        return s;

}

然后,我將getCompanyDetails(cid)的成功方法更新為:

    success: function (result) {
         res = JSON.parse(result.d);
         $jq14('#cphMainLeft_txtAddress').val(res['CompanyAddress']);
         $jq14('#cphMainLeft_txtCountry').val(res['CompanyCountry']);
         $jq14('#cphMainLeft_txtCity').val(res['CompanyCity']);
         $jq14('#cphMainLeft_txtNewCompanyName').val(res['CompanyName']);
         $jq14('#cphMainLeft_txtEmail').val(res['CompanyEmail']);
         $jq14('#cphMainLeft_txtPanNo').val(res['CompanyPAN']);
         $jq14('#cphMainLeft_txtTinNo').val(res['CompanyTIN']);
         $jq14('#cphMainLeft_txtPhone').val(res['CompanyPhone']);
         $jq14('#cphMainLeft_txtPincode').val(res['CompanyPincode']);
         $jq14('#cphMainLeft_hfTxtCountry').val(res['CompanyCountry']);
         $jq14("#cphMainLeft_ddlCompanyType").val(res['CompanyType']);
     }

而且效果很好。 再次感謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM