Mvc 简单分页代码

     public ActionResult MyComment(string id, int page = 1)
        {
            string userid = EndUserLoginManage.Instance.loginUserID;

            ICommentInfoBLL c_bll = new CommentInfoBLL();
            int allcount = 0;
            int pageindex = (page < 1 ? 1 : page);
            int pagesize = 10;

            List<CommentInfo> CommentList = c_bll.GetListByUserID(userid, out allcount, pageindex, pagesize);
            if (CommentList != null && CommentList.Count > 0)
            {
                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 != 0) { pagecount += 1; }
    int prepage = (page == 1 ? 1 : page - 1);
    int nextpage = (page == pagecount ? pagecount : page + 1);
    int countbegin, countend;
    DataHelper.GetPageHelper(page, pagecount, 6, 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 - 1) / 2;
            int mod = (pagelength - 1) % 2;
            countbegin = curPage - halfpage;
            countend = curPage + halfpage + mod;
            if (countbegin < 1) { countend = countend + (1 - countbegin); countbegin = 1; }
            if (countend > allPage) { countbegin = countbegin - (countend - allPage); countend = allPage; }
            countbegin = countbegin < 1 ? 1 : countbegin;
            countend = countend > allPage ? allPage : countend;
        }
        #endregion










<div class="GRZX_sc_fg">
                <p>
                    @if (page > 1)
                    {
                        <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>

 

posted @ 2015-03-19 11:18  混世小郎君  阅读(399)  评论(0编辑  收藏  举报