C# ASP.NET MVC 5序列化类型为';的对象时检测到循环引用;数据项';
我在ASP.NET MVC项目中遇到循环引用错误 这是模型C# ASP.NET MVC 5序列化类型为';的对象时检测到循环引用;数据项';,c#,json,asp.net-mvc,asp.net-mvc-5,C#,Json,Asp.net Mvc,Asp.net Mvc 5,我在ASP.NET MVC项目中遇到循环引用错误 这是模型 public partial class Item { public Item() { this.Inventories = new HashSet<Inventory>(); this.PurchasesDetails = new HashSet<PurchasesDetail>(); this.SalesDetails = new HashSe
public partial class Item
{
public Item()
{
this.Inventories = new HashSet<Inventory>();
this.PurchasesDetails = new HashSet<PurchasesDetail>();
this.SalesDetails = new HashSet<SalesDetail>();
}
public int Id { get; set; }
public string Code { get; set; }
public int CategoryID { get; set; }
public string Name { get; set; }
public int MeasurementID { get; set; }
public int Quantity { get; set; }
public decimal BuyPrice { get; set; }
public decimal SalePrice { get; set; }
public decimal CommisionRate { get; set; }
public Nullable<System.DateTime> MftDate { get; set; }
public Nullable<System.DateTime> ExpDate { get; set; }
public Nullable<int> StockLimit { get; set; }
public string Description { get; set; }
public string UserID { get; set; }
public virtual AspNetUser AspNetUser { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Inventory> Inventories { get; set; }
public virtual Measurement Measurement { get; set; }
public virtual ICollection<PurchasesDetail> PurchasesDetails { get; set; }
public virtual ICollection<SalesDetail> SalesDetails { get; set; }
}
我尝试了
db.Configuration.ProxyCreationEnabled=false代码>但它不工作。非常感谢您的帮助。在global.asax中,将以下代码用于全局Json序列化设置:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
尝试使用以下命令更改代码的以下部分:
string json = JsonConvert.SerializeObject(items, Formatting.None);
return Json(new { data = json }, JsonRequestBehavior.AllowGet);
我也遇到了同样的问题,我试过这个
var plan_master = from s in db.Plan_Master select s;
var plans = plan_master.Select(S => new
{
S.Plan_ID,
S.Plan
});
return Json(new { data = plans }, JsonRequestBehavior.AllowGet);
我收到此错误“Newtonsoft.Json.JsonSerializationException:”检测到类型为“Data.Item”的自引用循环。路径“[0].AspNetUser.Items.”
var plan_master = from s in db.Plan_Master select s;
var plans = plan_master.Select(S => new
{
S.Plan_ID,
S.Plan
});
return Json(new { data = plans }, JsonRequestBehavior.AllowGet);