序列化

时间:2017-05-22 09:28:45

标签: c# jquery asp.net ajax webforms

我正在尝试通过ajax根据公司ID获取公司数据,并使用收到的数据填充相关的文本框。通过ajax自动填充选择公司ID。两天前代码工作正常,突然它开始生成一个错误,只有前两个自动完成条目和休息它工作正常。任何人都可以指出这个错误。谢谢。

错误详细信息:"消息":"在序列化类型为\ u0027System.Data.Entity.DynamicProxies.Company_81625299B5B4D733336D55E48BE84921728B8D48335366DF8CA6844A8D10FF5D \ u0027的对象时检测到循环引用。 ""堆栈跟踪":"在System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o,StringBuilder sb,Int32 depth,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();
    }

1 个答案:

答案 0 :(得分:0)

感谢Ashokkumar M. Prajapati提供的提示。

我没有从[WebMethod]返回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方法更新为:

    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']);
     }

它运作得非常好。再次感谢。