【发布时间】:2016-01-31 10:11:36
【问题描述】:
我在控制器中的操作
在实体t_panier(表示购物车)中包含一个包含购物车列表的对象产品(panires):
public ActionResult Indexvm()
{
int iduser = 0;
iduser = (int)Session["idUser"];
IEnumerable<t_panier> p = panSer.getAllProducts(iduser).AsEnumerable();
return Json(p, JsonRequestBehavior.AllowGet);
}
我收到此错误:
“/”应用程序中的服务器错误。
在序列化类型对象时检测到循环引用 'System.Data.Entity.DynamicProxies.t_product_8A57351FB72B09DE03561C95AF908FE99EBFF074229BDC8C61D7917D53F43A28'。
t_panier:
public class t_panier
{
public int id { get; set; }
public int Qu { get; set; }
public double total { get; set; }
public Nullable<int> customer_id { get; set; }
public Nullable<int> product_fk { get; set; }
public virtual t_customer customer { get; set; }
public virtual t_product product { get; set; }
}
t_product :
public partial class t_product
{
public t_product()
{
this.t_order_line = new List<t_order_line>();
this.t_review = new List<t_review>();
}
public int id { get; set; }
[Required(ErrorMessage = "Name is required")]
[StringLength(25, ErrorMessage = "Must be less than 25 characters")]
[MaxLength(50)]
public string title { get; set; }
[DataType(DataType.MultilineText)]
public string detail { get; set; }
[DataType(DataType.Currency)]
[Range(0, double.MaxValue)]
public Nullable<float> price { get; set; }
[Range(0, int.MaxValue)]
public int quantity { get; set; }
public Nullable<int> category_id { get; set; }
public Nullable<int> vendor_id { get; set; }
public virtual t_category t_category { get; set; }
public virtual ICollection<t_order_line> t_order_line { get; set; }
public virtual ICollection<t_review> t_review { get; set; }
public virtual t_vendor t_vendor { get; set; }
[Display(Name = "Image")]
[DataType(DataType.ImageUrl)]
public string image { get; set; }
public virtual ICollection<t_evaluation> evaluations { get; set; }
public virtual ICollection<t_pan> carts { get; set; }
public virtual ICollection<t_panier> panires { get; set; }
}
【问题讨论】:
-
错误是不言自明的。你有一个循环引用。你需要展示你的模型。
-
t_panier包含t_product其中包含t_panier(即循环引用)。不要返回t_panier。而是返回一个匿名对象的集合,其中仅包含您在视图中需要的那些属性(使用p.Select(x => new { id= x.id, Qu = x.Qu, ..... });
标签: c# asp.net json asp.net-mvc