public class PageBarHelper//分页类 { public static string GetPageBar(string requestHref,int totalCount,int pageIndex,int pageSize) { //初始化pageSize,如果为0,默认为5 pageSize = pageSize == 0 ? 5 : pageSize; pageIndex = pageIndex == 0 ? 1 : pageIndex; //计算总页数 int pageCount = Convert.ToInt32(Math.Ceiling((double)(totalCount / pageSize))); //构建输出字符串 StringBuilder sb = new StringBuilder(); sb.AppendFormat("<div id='pageBar'>"); //判断pageCount是否足够分页,足够分页时 if (pageCount > 1) { if (pageIndex!=1) //如果当前页不是第一页,显示首页标识 { sb.AppendFormat("<a href='{0}?pageIndex=1&&pageSize={1}'>首 页", requestHref, pageSize); } sb.Append(" "); if (pageIndex>1) //如果当前页大于1,显示上一页标识 { sb.AppendFormat("<a href='{0}?pageIndex={1}&&pageSize={2}'>上一页</a>" , requestHref, pageIndex - 1, pageSize); } sb.Append(" "); if (pageIndex!=pageCount) //如果当前页不是最后一页,显示下一页标识 { sb.AppendFormat("<a href='{0}?pageIndex={1}&&pageSize={2}'>下一页</a>" , requestHref, pageIndex + 1, pageSize); sb.AppendFormat("<a href='{0}?pageIndex={1}&&pageSize={2}'>末 页</a>" , requestHref, pageCount, pageSize); } sb.Append(" "); } sb.AppendFormat("第{0}页/共{1}页", pageIndex, pageCount); sb.AppendFormat("</div>"); return sb.ToString(); } }
//页面调用分页类的分页方法:要将传如的string类型转成HtmlString类型,采用@Html.Raw(string str)函数
@{
string href = "/UserInfo/Index/";
@Html.Raw(PageBarHelper.GetPageBar(href, (int)ViewData["recordcount"], (int)ViewData["pageIndex"], (int)ViewData["pageSize"]));
}