摘要
一直在用前后端分离,在一个后台管理的页面中,尝试封装了一个辅助类。
类
////// 分页viewmodel /// ///public class PagedVM where T : class, new() { private MysqlContext _mysqlContext; /// /// 构造函数注入数据库上下文 /// /// public PagedVM(MysqlContext mysqlContext) { _mysqlContext = mysqlContext; } public Expression> Where { get; set; } /// /// 分页条开始索引 /// public int Start { get { return PageIndex - 10 > 0 ? PageIndex - 10 : 1; } } ////// 分页条结束索引 /// public int End { get { return PageIndex + 10 > PageCount ? PageCount + 1 : PageIndex + 10; } } public int PageIndex { set; get; } = 1; public int PageSize { set; get; } = 10; public bool HavePrevious { get { return PageIndex > 1; } } public bool HaveNext { get { return PageIndex < PageCount; } } public int PageCount { get { return (int)Math.Ceiling(TotalCount * 1.0 / PageSize); } } public int TotalCount { get { return _mysqlContext.Set().Count(); } } public List Items { get { if (Where != null) { return _mysqlContext.Set ().Where(Where).ToList().Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList(); } return _mysqlContext.Set ().ToList().Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList(); } } }
分布试图分页条
测试