[英]A circular reference was detected while serializing an object of type Residential Building
嘗試進行AJAX調用時收到以下異常。 我不知道這是什么
這是控制器的代碼:
public JsonResult getBldgInfo(string type, short stories, int size)
{
var info = from r in db.ResidentialBuildings
where r.type == type
&& r.stories == stories
&& r.size == size
select new { r };
return Json(info, JsonRequestBehavior.AllowGet);
}
這是AJAX調用:
$("#ResidentialBuildingSize").change(function () {
infoHead = "infoHead";
invisible = "invisible";
type = $("#ResidentialBuildingType").val();
stories = $("#ResidentialBuildingStories").val();
size = $("#ResidentialBuildingSize").val();
console.log(type);
console.log(stories);
console.log(size);
url = "/ResidentialBuilding/getBldgInfo?type=" + type + "&stories=" + stories + "&size=" + size;
console.log(url);
$.getJSON(url, function (data) {
console.log(data);
$.each(data, function (index, value) {
$("#titles").empty();
$("#values").empty();
$("#titles").append('<h5 class="' + infoHead + '">' + value.r.type + " (" + value.r.stories + ")" + '</h5>');
$("#titles").append('<p>' + "Stories" + '</p>');
$("#titles").append('<p>' + "Age" + '</p>');
$("#titles").append('<p>' + "Size" + '</p>');
$("#titles").append('<p>' + "Orientation" + '</p>');
$("#titles").append('<p>' + "Shape" + '</p>');
$("#titles").append('<p>' + "Floor Height" + '</p>');
$("#titles").append('<p>' + "Foundation" + '</p>');
$("#titles").append('<p>' + "Window Percent" + '</p>');
$("#titles").append('<p>' + "Heating" + '</p>');
$("#titles").append('<p>' + "Cooling" + '</p>');
$("#values").append('<h5 class="' + infoHead + ' ' + invisible + '">' + ' ' + '</h5>');
$("#values").append('<p><strong>' + value.r.stories + '</strong></p>');
$("#values").append('<p><strong>' + value.r.age + '</strong></p>');
$("#values").append('<p><strong>' + value.r.size + '</strong></p>');
$("#values").append('<p><strong>' + value.r.orientation + '</strong></p>');
$("#values").append('<p><strong>' + value.r.shape + '</strong></p>');
$("#values").append('<p><strong>' + value.r.floorht + '</strong></p>');
$("#values").append('<p><strong>' + value.r.foundation + '</strong></p>');
$("#values").append('<p><strong>' + value.r.windowpercent + '</strong></p>');
$("#values").append('<p><strong>' + value.r.heating + '</strong></p>');
$("#values").append('<p><strong>' + value.r.cooling + '</strong></p>');
});
});
});
$("#info").click(function () {
$("#bldgInfo").slideToggle('2000', 'linear');
});
如果我做錯了任何事情,請告訴我。 當我使用本地數據庫時,此確切的代碼有效。
更新
這是ResidentialBuilding模型的代碼。
namespace BIRDS.Models
public partial class ResidentialBuilding
{
public ResidentialBuilding()
{
this.ResidentialDatas = new HashSet<ResidentialData>();
}
public int Id { get; set; }
public string type { get; set; }
public short stories { get; set; }
public int size { get; set; }
public string age { get; set; }
public string orientation { get; set; }
public string shape { get; set; }
public int floorht { get; set; }
public string foundation { get; set; }
public int windowpercent { get; set; }
public string heating { get; set; }
public string cooling { get; set; }
public virtual ICollection<ResidentialData> ResidentialDatas { get; set; }
}
}
可能您的模型中具有導航屬性,因此序列化程序會遵循它們並從鏈接的模型中獲取對模型的引用,如果沒有循環引用檢測機制,則此過程將無限期繼續。
要解決您的問題,請僅選擇您感興趣的屬性:
var info = from r in db.ResidentialBuildings
where r.type == type
&& r.stories == stories
&& r.size == size
select new { r.stories, r.age, r.size /* etc. */ };
我敢冒險說,您的ResidentialData
類還具有對ResidentialBuilding
類的引用。 當您將其序列化為JSON時, JavaScriptSerializer
看到對集合的引用並開始序列化集合。 在ResidentialData
類中,它將看到對原始ResidentialBuilding
類的引用-並且它將繼續進行序列化,直到永遠。 您需要做的是讓序列化程序忽略其中兩個成員,具體取決於您的需求。
您可以通過在項目中添加對System.Web.Extensions
程序集的引用來實現。 從這里,您將添加對序列化類的引用( using System.Web.Script.Serialization;
)。
最后,您可以使用ScriptIgnoreAttribute
標志,這樣它將不再序列化該集合,並且不會拋出循環引用。 新類應如下所示:
using System.Web.Script.Serialization;
public partial class ResidentialBuilding
{
public ResidentialBuilding()
{
this.ResidentialDatas = new HashSet<ResidentialData>();
}
public int Id { get; set; }
public string type { get; set; }
public short stories { get; set; }
public int size { get; set; }
public string age { get; set; }
public string orientation { get; set; }
public string shape { get; set; }
public int floorht { get; set; }
public string foundation { get; set; }
public int windowpercent { get; set; }
public string heating { get; set; }
public string cooling { get; set; }
[ScriptIgnoreAttribute]
public virtual ICollection<ResidentialData> ResidentialDatas { get; set; }
}
我猜你是:
public virtual ICollection<ResidentialData> ResidentialDatas { get; set; }
Is和Entity Framework的導航屬性,而ResidentialData類的導航屬性返回到ResidentialBuilding。
您可以使用DTO來解決此問題,也許可以使用AutoMapper之類的庫在類型之間進行映射,並根據需要忽略或添加屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.