mvc EF框架中,加载外键对象序列化对象时报错 序列化类型为XX的对象时检测到循环引用...

本文介绍如何利用Newtonsoft.Json库将C#中的对象序列化为JSON格式,并展示了一个具体的例子,即序列化书籍列表并返回给客户端的过程。同时,还提供了easyUI的实现方式以展示这些数据。

Newtonsoft.Json.dll 或者通过-》工具-》库程序包管理工具-》NuGet管理包-》联机 输入Newtonsoft或者json.net

Newtonsoft.Json是可以的:

 

context.Response.ContentType = "text/plain";
BooksService service = new BooksService();
List<Books> list=service.GetAll().Take(5).ToList();
JsonSerializerSettings setting = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};

strintret = JsonConvert.SerializeObject(list, setting);
context.Response.Write(ret);

 

 

easyui 代码

 

 

$('#BooKlist').datagrid({
url: 'BookHandler.ashx?flag=GetBookList',
pagination: true,
pageList: [5, 15, 20, 25],
pageSize: 5,
columns: [[
{ field: 'Title', title: '书名', width: 120 },
{ field: 'Author', title: '作者', width: 120 },
{ field: 'ISBN', title: 'ISBN', width: 120 },
{
field: 'Publishersid', title: '出版社', width: 120,
formatter: function (value, row, index) {
return row.Publishers.Name;
}

},
{
field: 'CategoryId', title: '书类别', width: 120,
formatter: function (value, row, index) {
return row.Categories.Name;
}
}

]]
});

转载于:https://www.cnblogs.com/wlzhang/p/8994243.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值