Asp核心,如何将PagingList< T> .CreateAsync()与viewModel一起使用? [英] Asp Core, How to use PagingList<T>.CreateAsync() with a viewModel?

查看:85
本文介绍了Asp核心,如何将PagingList< T> .CreateAsync()与viewModel一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a asp.net core 1.1 project and i want to create paging in my some views. I studied microsoft documents about paging in asp core but it is very simple mode with 1 table. In my view i use multi table and use a viewmodel to initialize it. I want to use PagingList<T>.CreateAsync() method to create paging but get error: 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视图中分页的内容,这是viewmodel的简单列表

I have not yet written anything about paging in index view and it is a simple list of viewmodel

推荐答案

从发布的代码中确实无法确定.但例外情况是,CreateAsync方法需要IOrderedQueryable,但是您要为其提供IQueryable.

Well can't really be sure from the code you posted. But the exception says the the CreateAsync method needs IOrderedQueryable, but you're giving it an IQueryable.

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

Try changing it to pass in the query object (which I guess should implement the IOrderedQueryable, if you're using Entity framework).

PagingList背后的想法(大概)是使用它在数据库中进行分页.
您正在做的是将经过过滤的集合放入内存中(当通过结果进行逐个访问时),然后进行分页.

The idea behind the PagingList (presumably) is to use it to do the paging in the database.
What you're doing is bringing the filterted set into memory (when for-eaching through the result), and then doing doing the paging.

代码可能看起来像这样:

The code might look something like this:

[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);
}

希望有帮助.

这篇关于Asp核心,如何将PagingList&lt; T&gt; .CreateAsync()与viewModel一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆