本文介绍了Asp Core,如何创建分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PagingList<T>.CreateAsync()在索引视图中创建PagedList,但在var modelPaging = await PagingList<UserListViewModel>.CreateAsync(model, 10, page);行中得到以下错误:

I want to use PagingList<T>.CreateAsync() to create PagedList in index view but get below error in line var modelPaging = await PagingList<UserListViewModel>.CreateAsync(model, 10, page);:

我的操作代码:

    [HttpGet]
    public async Task<IActionResult> Index(int page = 1)
    {

        List<UserListViewModel> model = new List<UserListViewModel>();
        model = _userManager.Users.AsNoTracking().Select(u => new UserListViewModel
        {
            Id = u.Id,
            FullName = u.FirstName + " " + u.LastName,
            Email = u.Email
        }).OrderBy(u => u.Id).ToList();
        var modelPaging = await PagingList<UserListViewModel>.CreateAsync(model, 10, page);

        return View(modelPaging);
    }

和UserListViewModel Viewmodel:

public class UserListViewModel
{

    public string Id { get; set; }

    public string FullName { get; set; }

    public string Email { get; set; }

    public string RoleName { get; set; }

}

我应该对代码进行哪些更改?

What changes should I make in my code?

我在这里

推荐答案

该错误非常清楚,告诉您CreateAsync需要IOrderedQueryable,但是您要提供已经从数据库中检索到的数据.您应该传递查询本身.删除ToList;

The error is pretty clear that tells you CreateAsync needs IOrderedQueryable but you are giving the data already retrieved from database. You should pass the query itself. Remove ToList;

var query = _userManager.Users.AsNoTracking().Select(u => new UserListViewModel
{
    Id = u.Id,
    FullName = u.FirstName + " " + u.LastName,
    Email = u.Email
}).OrderBy(u => u.Id);
var modelPaging = await PagingList<UserListViewModel>.CreateAsync(query, 10, page);

PagingList的主要目的是直接从数据库而不是在内存中应用分页.

The main purpose of PagingList apply the pagination directly from database instead of in-memory.

这篇关于Asp Core,如何创建分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 04:31