Asp Core,如何创建Paging?
Asp Core, How to create Paging?
我想使用 PagingList<T>.CreateAsync()
在索引视图中创建 PagedList 但在行 var modelPaging = await PagingList<UserListViewModel>.CreateAsync(model, 10, page);
:
中出现以下错误
can not convert from system.collection.Generic.List<> to system.linq.IorderedQueryable<>
我的操作代码:
[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 视图模型:
public class UserListViewModel
{
public string Id { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string RoleName { get; set; }
}
我应该对我的代码进行哪些更改?
我在这里问了另一个关于这个问题的问题
错误很明显,告诉您 CreateAsync
需要 IOrderedQueryable
但您提供的数据已经从数据库中检索。您应该传递查询本身。删除 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
的主要目的是直接从数据库而不是内存中应用分页。
我想使用 PagingList<T>.CreateAsync()
在索引视图中创建 PagedList 但在行 var modelPaging = await PagingList<UserListViewModel>.CreateAsync(model, 10, page);
:
can not convert from system.collection.Generic.List<> to system.linq.IorderedQueryable<>
我的操作代码:
[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 视图模型:
public class UserListViewModel
{
public string Id { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string RoleName { get; set; }
}
我应该对我的代码进行哪些更改?
我在这里问了另一个关于这个问题的问题
错误很明显,告诉您 CreateAsync
需要 IOrderedQueryable
但您提供的数据已经从数据库中检索。您应该传递查询本身。删除 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
的主要目的是直接从数据库而不是内存中应用分页。