是否有任何简单的方法可以在PagedList
Nuget包中的PagedList.Mvc
等asp.net样板解决方案中的表示层中实现分页。
我从Nuget gallery安装了PagedList.Mvc
包,并将我的Index方法更改为:
public async Task<ActionResult> Index(int? page)
{
var pageIndex = (page ?? 1) - 1; //NewsService expects a 0 for the first page
var pageSize = 3;
int totalUserCount; // will be set by call to GetAllNews due to _out_ paramter :-|
var newsResult = await _newsAppService.GetAllNews(new PagedResultRequestDto { MaxResultCount = pageSize });
totalUserCount = newsResult.TotalCount;
var news = newsResult.Items;
var newsAsIPagedList = new StaticPagedList<NewsDto>(news, pageIndex + 1, pageSize, totalUserCount);
ViewBag.OnePageOfNews = newsAsIPagedList;
var model = new NewsListViewModel
{
News = news
};
return View(model);
}
在视图中我在新闻表下添加:
@Html.PagedListPager((IPagedList)ViewBag.OnePageOfNews, page => Url.Action("Index", new { page }))
但是当我点击数字,例如数字2或3时,表格刷新但更改数据。
答案 0 :(得分:1)
是的,您可以从演示文稿视图中进行样板应用程序,如果它很小,那么您可以使用bootstrap datatable如果不是那么您可以使用手册
对于分页,您可以使用手动类和列表扩展功能
public static class ListExtensions
{
public static PaginatedList<T> ToPagedList<T>(this IQueryable<T> list, int page, int pageSize)
{
int count = list.Count();
var items = list.Skip((page - 1) * pageSize).Take(pageSize).ToList();
return new PaginatedList<T>(items, count, page, pageSize);
}
public static async Task<PaginatedList<T>> ToAsycPagedList<T>(this IQueryable<T> list, int page, int pageSize)
{
return await PaginatedList<T>.CreateAsync(list, page, pageSize);
}
}
和像这样的分页类
public class PaginatedList<T> : List<T>// where T: IQueryable<T>, IEnumerable<T>, ICollection<T>
{
public int PageIndex { get; private set; }
public int TotalPages { get; private set; }
public int RowCount { get; set; }
public int PageSize { get; set; }
public int FirstRowOnPage
{
get { return (PageIndex - 1) * PageSize + 1; }
}
public int FirstPage
{
get { return 1; }
}
public int LastRowOnPage
{
get { return Math.Min(PageIndex * PageSize, RowCount); }
}
public int LastPage
{
get { return (int)Math.Floor((decimal)RowCount / PageSize); }
}
public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
RowCount = count;
PageSize = pageSize;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
this.AddRange(items);
}
public bool HasPreviousPage
{
get
{
return (PageIndex > 1);
}
}
public bool HasNextPage
{
get
{
return (PageIndex < TotalPages);
}
}
public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}
添加这两个类后,您可以将其用作
var data = await list.OrderBy(x => x.fieldname).ToAsycPagedList(page, pageSize);
var data = list.OrderBy(x => x.fieldname).ToPagedList(page, pageSize);
答案 1 :(得分:1)
ASP.NET Boilerplate不提供此功能,但如您所建议的那样使用PagedList.Mvc
非常简单。
可以轻松修改UsersController.Index
以使用manual paging:
public async Task<ActionResult> Index(int? page)
{
var pageIndex = (page ?? 1) - 1;
var pageSize = 10;
int totalUserCount;
var skipCount = pageIndex * pageSize;
var pagedResult = await _userAppService.GetAll(new PagedResultRequestDto { MaxResultCount = pageSize, SkipCount = skipCount });
totalUserCount = pagedResult.TotalCount;
var users = pagedResult.Items;
var usersAsIPagedList = new StaticPagedList<UserDto>(users, pageIndex + 1, pageSize, totalUserCount);
ViewBag.OnePageOfUsers = usersAsIPagedList;
// Code below unchanged
var roles = (await _userAppService.GetRoles()).Items;
var model = new UserListViewModel
{
Users = users,
Roles = roles
};
return View(model);
}