2017-05-22 13 views
0

私は会社のIDに基づいて会社のデータに基づいてajaxを取得し、受信したデータで関連するテキストボックスに入力しようとしています。企業IDは、ajaxオートコンプリートによって選択されます。コードは2日後に正常に動作していました。突然、自動補完の最初の2つのエントリだけがエラーを生成し始め、残りの部分は正常に動作しています。誰もがこのミスを指摘できますか?ありがとうございました。タイプのオブジェクトをシリアル化している間に循環参照が検出されました

エラーの詳細: "メッセージ": "\ u0027System.Data.Entity.DynamicProxies.Company_81625299B5B4D7A3375D55E48BE84921728B8D48335366DF8CA6844A8D10FF5D \ u0027タイプのオブジェクトをシリアル化しながら、循環参照が検出されました。"、 "のStackTrace":」System.Webので。 。Script.Serialization.JavaScriptSerializer.SerializeValueInternal Rを\ n \(オブジェクトO、StringBuilderのSB、のInt32の深さ、ハッシュテーブルobjectsInUse、serializationFormat serializationFormat、のMemberInfo CURRENTMEMBER)以下

は私のコードである:

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(); 
    } 
+0

を参照してください。http://stackoverflow.com/questions/37281026/circular-reference-while-serializing-ef-model-to-json& http://stackoverflow.com/questions/26434738/how-do-あなたは本当にシリアライズして循環参照するオブジェクトnewtonsoft-json –

+0

あなたの '会社'モデルを投稿できますか? – Abi

答えて

0

はヒントを提供するために、あなたにAshokkumar M.プラジャーパティをありがとうございます。

[WebMethod]からCompanyオブジェクトを返すのではなく、会社オブジェクトをJson文字列に変換して返しました。

[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)の成功の方法を更新

へ:

は、ここに私のWebMethod属性である

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

、それは見事に働きました。再度、感謝します。

関連する問題