My action in the controller
In the entity t_panier
(means shopping cart) contains an object product that contains a list of carts(panires):
public ActionResult Indexvm()
{
int iduser = 0;
iduser = (int)Session["idUser"];
IEnumerable<t_panier> p = panSer.getAllProducts(iduser).AsEnumerable();
return Json(p, JsonRequestBehavior.AllowGet);
}
I get this error :
Server Error in '/' Application .
Circular reference detected while serializing an object of type '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; }
}
Yes, what you are trying to serialize contains a circular reference. What that means is that 2 objects are referencing one another, so it causes an endless loop.
To fix this, you have to tell the framework to explicitly handle it. Good info here: http://johnnycode.com/2012/04/10/serializing-circular-references-with-json-net-and-entity-framework/
var jsonSerializerSettings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
};
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonNetFormatter(jsonSerializerSettings));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.