Asp 核心,如何将 PagingList<T>.CreateAsync() 与 viewModel 一起使用?

Asp Core, How to use PagingList<T>.CreateAsync() with a viewModel?

我正在做一个 asp.net 核心 1.1 项目,我想在我的一些视图中创建分页。我研究了有关 asp 核心中分页的 Microsoft 文档,但它是 1 table 的非常简单的模式。在我看来,我使用 multi table 并使用 viewmodel 来初始化它。我想使用 PagingList<T>.CreateAsync() 方法创建分页但出现错误: can not convert from system.linq.Iqueryable<> to system.linq.IorderedQueryable<>

我的操作:

    [HttpGet]
    public async Task<IActionResult> Index(int? page)
    {
        List<BookListViewModel> model = new List<BookListViewModel>();
        var query = (from b in _context.books
                     join a in _context.authors on b.AuthorID equals a.AuthorId
                     join bg in _context.bookgroups on b.BookGroupID equals bg.BookGroupId


                     select new
                     {
                         b.BookId,
                         b.BookName,
                         b.BookPageCount,
                         b.BookImage,
                         b.AuthorID,
                         b.BookGroupID,
                         a.AuthorName,
                         bg.BookGroupName
                     });

        foreach (var item in query)
        {
            BookListViewModel objmodel = new BookListViewModel();

            objmodel.BookId = item.BookId;
            objmodel.BookName = item.BookName;
            objmodel.BookImage = item.BookImage;
            objmodel.BookPageCount = item.BookPageCount;
            objmodel.AuthorId = item.AuthorID;
            objmodel.BookGroupId = item.BookGroupID;
            objmodel.AuthorName = item.AuthorName;
            objmodel.BookGroupName = item.BookGroupName;

            model.Add(objmodel);

        }
        ViewBag.RootPath = "/upload/thumbnailimage/";
        int pageSize = 3;
        int pageNumber = (page ?? 1);

        return View(await PagingList<BookListViewModel>.CreateAsync(model.AsQueryable() , pageNumber, pageSize));
    }

我还没有在index视图中写任何关于分页的东西,它是一个简单的视图模型列表

从您发布的代码中无法确定。但是异常说 CreateAsync 方法需要 IOrderedQueryable,但你给它一个 IQueryable

尝试将其更改为传入 query 对象(如果您使用的是 Entity framework,我想应该实现 IOrderedQueryable)。

PagingList 背后的想法(大概)是用它在数据库中进行分页。
您正在做的是将过滤后的集合带入内存(当对结果进行 for-eaching 时),然后进行分页。

代码可能如下所示:

[HttpGet]
public async Task<IActionResult> Index(int page = 1)
{
  var query = (from b in _context.books
               join a in _context.authors on b.AuthorID equals a.AuthorId
               join bg in _context.bookgroups on b.BookGroupID equals bg.BookGroupId


               select new BookListViewModel()
               {
                 BookId = b.BookId,
                 BookName = b.BookName,
                 BookPageCount = b.BookPageCount,
                 BookImage = b.BookImage,
                 AuthorId = b.AuthorID,
                 BookGroupId = b.BookGroupID,
                 AuthorName = a.AuthorName,
                 BookGroupName = bg.BookGroupName
               }).AsNoTracking().OrderBy(u => u.BookId);


  ViewBag.RootPath = "/upload/thumbnailimage/";

  var pagedResult = await PagingList<BookListViewModel>.CreateAsync(query, 10, page);
  return View(pagedResult);
}

希望对您有所帮助。