Mvc 简单分页代码

时间:2023-03-08 16:39:54
Mvc 简单分页代码
     public ActionResult MyComment(string id, int page = )
{
string userid = EndUserLoginManage.Instance.loginUserID; ICommentInfoBLL c_bll = new CommentInfoBLL();
int allcount = ;
int pageindex = (page < ? : page);
int pagesize = ; List<CommentInfo> CommentList = c_bll.GetListByUserID(userid, out allcount, pageindex, pagesize);
if (CommentList != null && CommentList.Count > )
{
CommentMessageBLL ucmb = new CommentMessageBLL();
foreach (var item in CommentList)
{
item.Url = "/Estate/Detail/" + item.ProjectId;
//item.CommentMessageList = ucmb.GetListByParentId(item.Id);
}
}
ViewBag.Userid = userid;
ViewBag.CommentList = CommentList;//评论列表
ViewBag.Pageindex = pageindex;
ViewBag.Pagesize = pagesize;
ViewBag.Allcount = allcount;
return View();
} @{
ViewBag.Title = "我的评论";
Layout = "~/Views/Shared/_PersonalLayoutPage.cshtml";
string userid = ViewBag.Userid;
List<CommentInfo> CommentList = ViewBag.CommentList as List<CommentInfo>;
int page = ViewBag.Pageindex;
int pagesize = ViewBag.Pagesize;
int allcount = ViewBag.Allcount;
int pagecount = allcount / pagesize; if (allcount % pagesize != ) { pagecount += ; }
int prepage = (page == ? : page - );
int nextpage = (page == pagecount ? pagecount : page + );
int countbegin, countend;
DataHelper.GetPageHelper(page, pagecount, , out countbegin, out countend); } #region 页码计算函数
/// <summary>
/// 页码计算函数
/// </summary>
/// <param name="curPage">当前页码</param>
/// <param name="allPage">所有页数</param>
/// <param name="pagelength">要显示多少个页码</param>
/// <param name="countbegin">结果:开始页码(包括本身)</param>
/// <param name="countend">结果:结束页码(包括本身)</param>
public static void GetPageHelper(int curPage, int allPage, int pagelength, out int countbegin, out int countend)
{
int halfpage = (pagelength - ) / ;
int mod = (pagelength - ) % ;
countbegin = curPage - halfpage;
countend = curPage + halfpage + mod;
if (countbegin < ) { countend = countend + ( - countbegin); countbegin = ; }
if (countend > allPage) { countbegin = countbegin - (countend - allPage); countend = allPage; }
countbegin = countbegin < ? : countbegin;
countend = countend > allPage ? allPage : countend;
}
#endregion <div class="GRZX_sc_fg">
<p>
@if (page > )
{
<a href="?page=1">首页</a> <a href="?page=@(prepage)">上一页</a>
}
@for (int i = countbegin; i <= countend; i++)
{
if (i == page)
{
<a class="Cur01" href="?page=@(i)">@(i)</a>
}
else
{
<a href="?page=@(i)">@(i)</a>
}
}
@if (page < pagecount)
{
<a href="?page=@(nextpage)">下一页</a> <a href="?page=@(pagecount)">末页</a>
}
共@(pagecount)页</p>
</div>