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

问题描述 投票:0回答:1

我正在开发一个asp.net core 1.1项目,我想在我的一些视图中创建分页。我研究了关于asp core分页的微软文档,但它是非常简单的模式,只有1个表。在我看来,我使用多表并使用

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
视图中写任何关于分页的内容,它只是一个简单的视图模型列表

asp.net asp.net-mvc asp.net-core sorting pagination
1个回答
0
投票

从您发布的代码中无法真正确定。但例外情况是

CreateAsync
方法需要
IOrderedQueryable
,但你给它一个
IQueryable

尝试更改它以传递

query
对象(我猜应该实现
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);
}

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.