0

I am following the official ASP.NET Core 2.2 paging example over at Microsoft.

Here, I'm adding a PaginatedList.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace BlatantlyCopiedCode
{
    public class PaginatedList<T> : List<T>
    {
        public int PageIndex { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            TotalPages = (int)Math.Ceiling(count / (double)pageSize);

            this.AddRange(items);
        }

        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 1);
            }
        }

        public bool HasNextPage
        {
            get
            {
                return (PageIndex < TotalPages);
            }
        }

        public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
        {
            var count = await source.CountAsync();
            var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
            return new PaginatedList<T>(items, count, pageIndex, pageSize);
        }
    }
}

and adding this to my EF object Merge

[HttpGet("[action]")]
public async Task<PaginatedList<Merge>> Index(int? pageNumber)
{
    var merges = context.Merges;

    int pageSize = 20;
    return await PaginatedList<Merge>.CreateAsync(merges.AsNoTracking(), pageNumber ?? 1, pageSize);
}

When the result is serialized to JSON in the controller, the properties from PaginatedList<T> aren't serialized, only the List<Merge>. How can I force the properties from PaginatedList<T> to appear in the resulting JSON?

conciliator
  • 6,078
  • 6
  • 41
  • 66
  • I think this will help? https://stackoverflow.com/questions/9727836/json-net-getter-property-not-serialized –  May 01 '19 at 20:59
  • Thank you for your comment @Amy, although I'm not exactly sure I understand what you mean. Decorating the properties with `[JsonPropertyAttribute(DefaultValueHandling = DefaultValueHandling.Include)]` does not seem to do the trick, at least. – conciliator May 01 '19 at 21:36
  • Okay, that's what I had in mind, but I wasn't sure if that was the issue. –  May 01 '19 at 22:00
  • I think `PaginatedList` is just handled by serializer as an ordinary `List`, resulting in a json-array. Note that, in the docs, it wasn't supposed to be serialized into json. As a solution, I'd prefer just decorating a list rather than deriving from it. So it would be json object with a couple of fields for paging and the array itself. – tukaef May 01 '19 at 22:47

1 Answers1

3

The reason is that it inherits the List<T> which constrains the result to a list.Modify your code to below

public class PaginatedList<T> //: List<T>
{
    public int PageIndex { get; private set; }
    public int TotalPages { get; private set; }

    public List<T> Items { get; set; }

    public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
    {
        PageIndex = pageIndex;
        TotalPages = (int)Math.Ceiling(count / (double)pageSize);

        this.Items = new List<T>();
        this.Items.AddRange(items);
    }
    //...
 }
Ryan
  • 19,118
  • 10
  • 37
  • 53