标签:实例 命名空间 asp ges 实现 source 方法 model this
在同一个命名空间下:
1.定义接口
public interface IPagedList<T> : IList<T> { int PageIndex { get; } int PageSize { get; } int TotalCount { get; } int TotalPages { get; } bool HasPreviousPage { get; } bool HasNextPage { get; } }
注意:IList<T> 继承了ICollection<T>, IEnumerable<T>, Ienumerable
2.实现接口:
[Serializable]//讲当前类的实例序列化,便于网络中传输和保存 public class PagedList<T> : List<T>, IPagedList<T> { /// <summary> /// Ctor /// </summary> /// <param name="source">source</param> /// <param name="pageIndex">Page index</param> /// <param name="pageSize">Page size</param> public PagedList(IQueryable<T> source, int pageIndex, int pageSize) { int total = source.Count(); this.TotalCount = total; this.TotalPages = total / pageSize; if (total % pageSize > 0) TotalPages++; this.PageSize = pageSize; this.PageIndex = pageIndex; this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList()); } /// <summary> /// Ctor /// </summary> /// <param name="source">source</param> /// <param name="pageIndex">Page index</param> /// <param name="pageSize">Page size</param> public PagedList(IList<T> source, int pageIndex, int pageSize) { TotalCount = source.Count(); TotalPages = TotalCount / pageSize; if (TotalCount % pageSize > 0) TotalPages++; this.PageSize = pageSize; this.PageIndex = pageIndex; this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList()); } /// <summary> /// Ctor /// </summary> /// <param name="source">source</param> /// <param name="pageIndex">Page index</param> /// <param name="pageSize">Page size</param> /// <param name="totalCount">Total count</param> public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount) { TotalCount = totalCount; TotalPages = TotalCount / pageSize; if (TotalCount % pageSize > 0) TotalPages++; this.PageSize = pageSize; this.PageIndex = pageIndex; this.AddRange(source); } public int PageIndex { get; private set; } public int PageSize { get; private set; } public int TotalCount { get; private set; } public int TotalPages { get; private set; } public bool HasPreviousPage { get { return (PageIndex > 0); } } public bool HasNextPage { get { return (PageIndex + 1 < TotalPages); } } }
3.功能的实现(控制器中)
从数据源(数据库)查询数据 为Hus
var aHus = new PagedList<Entity>(Hus, command.Page - 1, command.PageSize);// Entity:需要分页的实体, Hus:查询后的总数据 var gridModel = new DataSourceResult { Data = aHus.Select(x => { // var m = x.ToModel(); var m=new EntityModel(); // EntityModel界面model 赋值。。。。 return m; }), Total = aHus.TotalCount }; return Json(gridModel);
可简写为:
var gridModel = new DataSourceResult { Data = new PagedList<Entity>(Hus, command.Page - 1, command.PageSize).Select(x => { //var m = x.ToModel(); var m=new EntityModel(); // EntityModel界面model 赋值。。。。 return m; }), Total = aHus.TotalCount }; return Json(gridModel);
注意:DataSourceResult类为 (功能为装数据)
public class DataSourceResult { public IEnumerable Data { get; set; } public int Total { get; set; } }
4.前端代码(省去)
标签:实例 命名空间 asp ges 实现 source 方法 model this
原文地址:http://www.cnblogs.com/bfwbfw/p/6363520.html