asp.net mvc3分页代码

 最近在学asp.net mvc3,与asp.net webForm比起来。确实能提高不少效率,特别是数据校验和数据源方面,基本上不用自己手写。但是总担心其entity framework框架,会不会影响程序运行效率,下面是我自己写的分页代码,欢迎大家指教

 

  public ActionResult Index(int page)
        {
            int pageSize = 1;
            var query=ob.news.OrderByDescending(news=>news.id).Skip(pageSize*(page-1)).Take(pageSize);
            pageing pageInfo = new pageing();
            pageInfo.pageSize = pageSize;
            pageInfo.curentPage = page;
            pageInfo.totalCount = ob.news.Count();
            pageInfo.coustomUrl = "/news?";
            ViewBag.Page = pageInfo;
            return View(query);
          
        }

 

public class pageing
    {  
        /// <summary>
        /// 每页数量
        /// </summary>
        public int pageSize { get; set; }
       /// <summary>
        /// 当前页
       /// </summary>
        public int curentPage{ get; set; }
        /// <summary>
        /// 总数量
        /// </summary>
        public int totalCount { get; set; }
        /// <summary>
        /// 定义页页连接
        /// </summary>
        public string coustomUrl { get; set; }
        /// <summary>
        /// 自定义页面参数名
        /// </summary>
        public string coustomPageParName { get; set; }
        public string WrtiePage()
        {
          
            int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
            if (totalPage>1)
            {
              
                StringBuilder strs = new StringBuilder();

                strs.AppendFormat("<a href=\"{0}{1}={2}\">上一页</a> ", coustomUrl, string.IsNullOrEmpty(coustomPageParName) ? "page" : coustomPageParName, curentPage == 1 ? 1 : curentPage - 1);
                for (int i = curentPage==1?1:curentPage-1; i <= curentPage+5; i++)
                    {
                        if (i == curentPage)
                        {
                            strs.AppendFormat("<span id='curentPage'>{0}</span> ",i);
                        }
                        else
                        {
                            strs.AppendFormat("<a href=\"{0}{1}={2}\">{2}</a> ", coustomUrl, string.IsNullOrEmpty(coustomPageParName) ? "page" : coustomPageParName, i);

                        }
                        if (i == totalPage || i==totalPage/5) { break; }
                    }
                    strs.AppendFormat("<a href=\"{0}{1}={2}\">下一页</a> ", coustomUrl, string.IsNullOrEmpty(coustomPageParName) ? "page" : coustomPageParName, curentPage == totalPage ? totalPage : curentPage + 1);

                return   strs.ToString();
            }
            return "";
        }
    }
}

posted @ 2013-03-09 11:50  fogwu  阅读(273)  评论(0编辑  收藏  举报