这是我的帮助类
/// <summary> /// 分页帮助类 /// </summary> /// <typeparam name="T"></typeparam> public class PagingHelper<T> { //分页数据源 public IEnumerable<T> DataSource { get; private set; } //每页显示记录的数量 public int PageSize { get; private set; } //当前页数 public int PageIndex { get; set; } //分页总页数 public int PageCount { get; private set; } //是否有前一页 public bool HasPrev { get { return PageIndex > 1; } } //是否有下一页 public bool HasNext { get { return PageIndex < PageCount; } } //构造函数 public PagingHelper(int pageSize, IEnumerable<T> dataSource) { this.PageSize = pageSize > 1 ? pageSize : 1; this.DataSource = dataSource; PageCount = (int)Math.Ceiling(dataSource.Count() / (double)pageSize); } //获取当前页数据 public IEnumerable<T> GetPagingData() { return DataSource.Skip((PageIndex - 1) * PageSize).Take(PageSize); } }
这是我的控制器方法
public ActionResult SelectGameArea(int pageIndex = 1) { IndexProvider areaProvider = new IndexSqlProvider(); DataTable dt = areaProvider.GameInfo_SelectGame(); List<GameInfoModels> areaList = new List<GameInfoModels>(); ViewBag.pageCount = dt.Rows.Count; foreach (DataRow dr in dt.Rows) { GameInfoModels gameArea = new GameInfoModels(); gameArea.GameAreaName = dr["GameAreaName"].ToString(); areaList.Add(gameArea); } PagingHelper<GameInfoModels> AreaPaging = new PagingHelper<GameInfoModels>(2,areaList); AreaPaging.PageIndex = pageIndex; return View(AreaPaging); }
这是我的需要分页的页面
@* @model IEnumerable<_1_Ars.SywMobile.Entity.Index.GameInfoModels> *@ @model IEnumerable<_5_Ars.SywMobile.Common.PagingHelper<_1_Ars.SywMobile.Entity.Index.GameInfoModels>> <!doctype html> <html> <head> <meta charset="utf-8"> <title>选择区服</title> <link href="@Url.Content("~/Content/themes/common.css")" type="text/css" rel="stylesheet" /> <link href="@Url.Content("~/Content/themes/gemearea.css")" rel="stylesheet" type="text/css"> </head> <body> <div id="header"> <span class="header_l"><a href="#">返回</a></span> <span class="header_r"><a href="#"> </a></span> </div> <div id="content"> <div class="gameArea"> <div class="header"> <span>选择区服</span> </div> <div class="gameArea_content"> <ul> @foreach (var item in Model) { <li><a href="">@item.GameAreaName</a></li> } </ul> </div> </div> <!--选择区服--> <form class="pageForm"> <div class="pageBtn"> <div class="pageBtn_left"> <input type="button" value="上一页"> </div> <div class="pageBtn_middle"> <select> <option value="1">第页</option> <option value="1">第一页</option> <option value="2">第二页</option> <option value="3">第三页</option> <option value="4">第四页</option> <option value="5">第五页</option> <option value="6">第六页</option> <option value="7">第七页</option> <option value="8">第八页</option> </select> </div> <div class="pageBtn_right"> <input type="button" value="下一页"> </div> </div> </form> </div>
问题是:在页面中我怎么引用分页帮助类,然后model.???这是最终要实现的效果
@model PagingHelper<GameInfoModels>