1:下载mvcpager分页控件 mvcpager.dll
2:写好公共的分页代码 引用 Webdiyer.WebControls.Mvc;
int PageSize = 10;
/// <summary>
/// 分页(每页记录数默认)
/// </summary>
/// <typeparam name="T">泛型TEntity实体</typeparam>
/// <param name="pageId">页码</param>
/// <returns>页数据列表</returns>
public PagedList<T> GetPageData<T>(int? pageId, IQueryable<T> query)
{
return GetPageData<T>(pageId, PageSize, query);
}
/// <summary>
/// 分页代码 /// </summary>
/// <typeparam name="T">泛型TEntity实体</typeparam>
/// <param name="pageId">页码</param>
/// <param name="pageSize">每页显示条数</param>
/// <returns>页数据列表</returns>
public PagedList<T> GetPageData<T>(int? pageId, int pageSize, IQueryable<T> query)
{
// 总页码数
int totalCount = query.Count();
int pageCount = (totalCount + pageSize - 1) / pageSize;
// 页码大于总页数
if (pageId.HasValue && pageId.Value > 1 && pageId.Value > pageCount)
pageId = pageCount;
int skipCount = pageId.HasValue && pageId.Value > 1 ? (pageId.Value - 1) * pageSize : 0;
ViewBag.SkipCount = skipCount;
// 页记录数据列表
PagedList<T> tList = new PagedList<T>(query.Skip(skipCount).Take(pageSize), pageId ?? 1, pageSize, totalCount);
return tList;
}
在 Controller页面:代码如下:
//分页页面
public ActionResult Index(int? id = 1)
{
List<User> list = GetUserList();
PagedList<User> pglist= GetPageData<User>(id, list.AsQueryable());
return View(pglist);
}
//获取数据集
public List<User> GetUserList()
{
List<User> user = new List<User>() {
new User(){ ID="0", Name="0", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="1", Name="1", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="2", Name="2", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="3", Name="3", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="4", Name="4", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="5", Name="5", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="6", Name="6", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="7", Name="7", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="8", Name="8", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="9", Name="9", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="10", Name="10", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="11", Name="11", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="12", Name="12", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="13", Name="13", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="14", Name="14", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="15", Name="15", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="16", Name="16", Sex="男" ,Phone="110", Address="0110100"},
new User(){ ID="17", Name="17", Sex="男" ,Phone="110", Address="0110100"},
}
/// <summary>
/// 定义实体
/// </summary>
public class User
{
public string ID { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
}
页面代码:
@model PagedList<MvcApplication.Controllers.User>
<h2>Index</h2>
<style type="text/css">
.table
{
width: 99%;
margin-left: 0.5%;
border-spacing: 1px;
text-align: center;
border: 1px solid #d9dadb;
}
.table th
{
color: #fff;
background-color:#77DDFF;
height: 29px;
font-weight: normal;
white-space: nowrap;
}
.table td { height: 29px; }
.table tr.odd { background-color: #fff; }
.table tr.even { background-color: #CCEEFF; }
.table tr.h { background-color: #e0e3e5; }
.table td a { text-decoration: none; }
</style>
<div id="pageList">
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Sex</th>
<th>phone</th>
<th>Address</th>
</tr>
@{
bool flag = false;
foreach (MvcApplication.Controllers.User f in Model)
{
flag = !flag;
<tr class="@(flag?"even":"odd")">
<td>@f.ID</td>
<td>@f.Name</td>
<td>@f.Sex</td>
<td>@f.Phone</td>
<td>@f.Address</td>
</tr>
}
}
</table>
<div class="pagerdiv">
<div>
共 @Model.TotalItemCount 条,页 @Model.CurrentPageIndex/ @Model.TotalPageCount
</div>
@Ajax.Pager(Model, new PagerOptions()
{
AutoHide = false,
CssClass = "floatright",
PageIndexParameterName = "id",
ShowMorePagerItems = false,
AlwaysShowFirstLastPageNumber = true,
ShowPageIndexBox = true,
PageIndexBoxWrapperFormatString = "页 {0}"
}, new AjaxOptions()
{
UpdateTargetId = "pageList"
})
</div>
</div>