【问题标题】:generate json from mvc 5 asp.net controller从 mvc 5 asp.net 控制器生成 json
【发布时间】: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 =&gt; new { id= x.id, Qu = x.Qu, ..... });

标签: c# asp.net json asp.net-mvc


【解决方案1】:

是的,您尝试序列化的内容包含循环引用。这意味着 2 个对象相互引用,因此会导致无限循环。

要解决此问题,您必须告诉框架显式处理它。好信息在这里: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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    相关资源
    最近更新 更多