.Net MVC 分页代码,分页的关键就是在于这几个参数pageIndex ,recordCount,pageSize ,下面是张林的网站做的一个简单的分页代码
效果如图 public class HtmlHelpers
{
/// <summary>
/// 获取分页html
/// </summary>
/// <param name="recordCount"></param>
/// <param name="pageSize"></param>
/// <param name="pageIndex"></param>
/// <param name="Path"></param>
/// <returns></returns>
public static string GetPagination(int recordCount, int pageSize, int pageIndex, string path)
{
int pageCount = recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1;
StringBuilder paginationHtml = new StringBuilder("<ul class='pagination'>");
if (pageCount>1)
{
//上一页
if (pageIndex == 1)
{
paginationHtml.Append("<li><span class='curPage'>上一页</a></li>");
}
else
{
paginationHtml.Append("<li><a href='" + path + (pageIndex-1) + "'>上一页</a></li>");
}
//一般页
for (int i = 1; i <= pageCount; i++)
{
if (i == pageIndex)
{
paginationHtml.Append("<li><span class='curPage'>" + i + "</span></li>");
}
else
{
paginationHtml.Append("<li><a href='" + path + i + "'>" + i + "</a></li>");
}
}
//下一页
if (pageIndex == pageCount)
{
paginationHtml.Append("<li><span class='curPage'>下一页</a></li>");
}
else
{
paginationHtml.Append("<li><a href='" + path + (pageIndex+1) + "'>下一页</a></li>");
}
paginationHtml.Append("</ul>");
}
return paginationHtml.ToString();
}
}