c# - genrate json from mvc 5 asp.net controller -


my action in controller

in entity t_panier (means shopping cart) contains object product contains 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 error :

server error in '/' application .

circular reference detected while serializing 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 required")]     [stringlength(25, errormessage = "must less 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, trying serialize contains circular reference. means 2 objects referencing 1 another, causes endless loop.

to fix this, have tell framework explicitly handle it. 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)); 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -