3

我正在尝试将 PagedList 对象(https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs)序列化为 Json,如下所示:

PagedList<Product> pagedList = new PagedList<Product>(products, (page - 1), pageSize);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(pagedList);

如果我使用上面的代码,结果我会得到一个正确序列化的 Product 对象数组。但是(PagedList 的)以下属性未包含在 Json 结果中:

    public bool HasNextPage { get; }
    public bool HasPreviousPage { get; }
    public bool IsFirstPage { get; }
    public bool IsLastPage { get; }
    public int ItemEnd { get; }
    public int ItemStart { get; }
    public int PageCount { get; }
    public int PageIndex { get; }
    public int PageNumber { get; }
    public int PageSize { get; }
    public int TotalItemCount { get; }

它们没有被序列化,但它们是 PagedList 的一部分。

有谁知道为什么?我怎么能在序列化中包含这些属性?

谢谢

4

4 回答 4

9

序列化程序看到 PagedList 是可枚举的,因此将其序列化为 JavaScript 数组。为了使这更容易处理,我GetMetaData()在 PagedList 对象上公开了一个函数,该函数将返回一个 MetaData 对象,其中包含您上面提到的字段。这意味着您可以像这样序列化您的页面列表:

string json = Newtonsoft.Json.JsonConvert.SerializeObject(new{
  items = pagedList,
  metaData = pagedList.GetMetaData()
});

这应该会产生一个 JSON 对象,如下所示:

{
    "Items": [
        { ... },
        { ... },
        { ... }
    ],
    "MetaData": {
        "PageSize": 1,
        "PageCount": 2,
        "HasNextPage": true,
        ...
    }
}
于 2012-06-21T04:06:30.913 回答
0

有一个 jquery 插件用于兴奋地做到这一点:

https://github.com/turhancoskun/pagify.mvc

<script type="text/javascript">
$(function() {
    $('#users').pagify({
        dataUrl: '/User/UserLis',
        callBack: function(){
           // Ajax remove preloader and some other callbacks  
        },
        beforeSend: function(){
           // Ajax show preloader and some other function before start
        }
    });
}
</script>

readme.md 文件包含一个示例

于 2013-07-12T12:57:21.517 回答
0

要序列化,只需使用 Troy 的实现。但是要反序列化创建 2 个新类:

public class PaginationEntity<TEntity> where TEntity : class 
{
    public PaginationEntity()
    {
        Items = new List<TEntity>();
    }
    public IEnumerable<TEntity> Items { get; set; }
    public PaginationMetaData MetaData { get; set; }
}

public class PaginationMetaData
{
    public int Count { get; set; }
    public int FirstItemOnPage { get; set; }
    public bool HasNextPage { get; set; }
    public bool HasPreviousPage { get; set; }
    public bool IsFirstPage { get; set; }
    public bool IsLastPage { get; set; }
    public int LastItemOnPage { get; set; }
    public int PageCount { get; set; }
    public int PageNumber { get; set; }
    public int PageSize { get; set; }
    public int TotalItemCount { get; set; }
}

并以这种方式反序列化:

PaginationEntity<TEntity> listPaginated = JsonConvert.DeserializeObject<PaginationEntity<TEntity>>(json)

你很高兴。

于 2016-08-12T02:24:39.193 回答
0

默认情况下,asp.core 使用 DataContractSerializer,因此任何人都可以设置Product 类[DataContract][DataMember]成员,并通过以下操作获取所需的数据:

 public IActionResult Products(int page=1,int countPerPage=10)
    {
        var result = _dbContext.Products.AsNoTracking().ToPagedList(page, countPerPage);

        return Ok( new{result, total=result.TotalItemCount });
    }

无需使用 Newtonsoft.Json

于 2019-04-23T12:00:54.863 回答