asp.net mvc 分页

手学类库,

//css样式表

    <style>
        ul.pagination {
            display: inline-block;
            padding: 0;
            margin: 0;
        }

            ul.pagination li {
                display: inline;
            }

                ul.pagination li a {
                    color: black;
                    float: left;
                    padding: 8px 16px;
                    text-decoration: none;
                    transition: background-color .3s;
                    border: 1px solid #ddd;
                }

                    ul.pagination li a.active {
                        background-color: #4CAF50;
                        color: white;
                        border: 1px solid #4CAF50;
                    }

                    ul.pagination li a:hover:not(.active) {
                        background-color: #ddd;
                    }
    </style>

//分页帮助类,

namespace UI.Models
{
    //PageIndex从0开始
    public interface IPageOfList
    {
        long CurrentStart { get; }
        long CurrentEnd { get; }
        int PageIndex { get; set; }
        int PageSize { get; set; }
        int PageTotal { get; }
        long RecordTotal { get; set; }
    }

    //包装从数据库中分页查找实体集,返回一个PageOfList<T>的对象,

//主要让List<T> 实体集添加PageIndex,PageSize,PageTotal,RecordTotal等属性;
    public class PageOfList<T> : List<T>, IPageOfList
    {
        
        public PageOfList(IEnumerable<T> items, int pageIndex, int pageSize, long recordTotal)
        {
            if (items != null)
            {
                AddRange(items);
            }
            this.PageIndex = pageIndex;
            this.PageSize = pageSize;
            this.RecordTotal = recordTotal;

        }

        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public long RecordTotal { get; set; }
        public int PageTotal
        {
            get { return (int)RecordTotal / PageSize + (RecordTotal % PageSize > 0 ? 1 : 0); }
        }
        public long CurrentStart
        {
            get
            {
                return PageIndex * PageSize + 1;
            }
        }
        public long CurrentEnd
        {
            get
            {
                return
           (PageIndex + 1) * PageSize >
          RecordTotal ? RecordTotal : (PageIndex + 1) * PageSize;
            }
        }
    }
    //分页导航栏实现

 

    //count, 让用户选择的页码个数默认为五个,当前选中的页面尽量往中间靠
    public static class  FenYeDaoHangLan
    {
        public static MvcHtmlString UIPaging<T>(HtmlHelper helper, PageOfList<T> list, int count = 5)
        {
            
            StringBuilder sb = new StringBuilder();
            if (list == null)
            {
                return new MvcHtmlString(sb.ToString());
            }
            //显示分页信息
            sb.AppendLine(string.Format("<div>共{0}条记录,每页{1}条,共{2}页</div>", list.RecordTotal, list.PageSize, list.PageTotal));
            sb.AppendLine(
              "<ul class='pagination'>"
              );
          
            System.Web.Routing.RouteValueDictionary route =
                                   new System.Web.Routing.RouteValueDictionary();
            foreach (var key in helper.ViewContext.RouteData.Values.Keys)
            {
                route[key] = helper.ViewContext.RouteData.Values[key];
            }
            foreach (string key in helper.ViewContext.RequestContext.HttpContext.Request.QueryString)
            {
                route[key] = helper.ViewContext.HttpContext.Request.QueryString[key];

            }
            route["pageIndex"] = list.PageIndex == 0 ? 0 : list.PageIndex - 1;
            sb.Append("<li>");
            sb.AppendLine(helper.ActionLink("<", route["action"].ToString(), route).ToHtmlString());
            sb.Append("</li>");
            //route["pageIndex"] = list.PageIndex == list.PageTotal-1 ? list.PageTotal-1 : list.PageIndex + 1;
            //sb.AppendLine(helper.ActionLink("下一页", route["action"].ToString(), route).ToHtmlString());
            if (list.PageTotal <= count)
            {
                for (int i = 0; i < list.PageTotal; i++)
                {
                    if (list.PageIndex == i)
                    {
                        sb.AppendLine("<li><a href='#' class='active' >" + (i + 1) + "</a></li>");
                        continue;
                    }
                    route["pageIndex"] = i;
                    sb.Append("<li>");
                    sb.AppendLine(helper.ActionLink(i + 1 + "", route["action"].ToString(), route).ToHtmlString());
                    sb.Append("</li>");
                }
                route["pageIndex"] = list.PageIndex == list.PageTotal - 1 ? list.PageTotal - 1 : list.PageIndex + 1;
                sb.Append("<li>");
                sb.AppendLine(helper.ActionLink(">", route["action"].ToString(), route).ToHtmlString());
                sb.Append("</li>");
                sb.AppendLine("</ul>");
                return new
                    MvcHtmlString(sb.ToString());

            }


            if (list.PageIndex < (count + 1) / 2)
            {
                for (int i = 0; i < count; i++)
                {
                    if (list.PageIndex == i)
                    {
                        sb.AppendLine("<li><a href='#' class='active' >" + (i + 1) + "</a></li>");
                        continue;
                    }
                    route["pageIndex"] = i;
                    sb.Append("<li>");
                    sb.AppendLine(helper.ActionLink(i + 1 + "", route["action"].ToString(), route).ToHtmlString());
                    sb.Append("</li>");
                }
                route["pageIndex"] = list.PageIndex == list.PageTotal - 1 ? list.PageTotal - 1 : list.PageIndex + 1;
                sb.Append("<li>");
                sb.AppendLine(helper.ActionLink(">", route["action"].ToString(), route).ToHtmlString());
                sb.Append("</li>");
                sb.AppendLine("</ul>");
                return new MvcHtmlString(sb.ToString());
            }
            if (list.PageIndex >= (count + 1) / 2 && list.PageIndex < list.PageTotal - (count - 1) / 2)
            {
                for (int i = list.PageIndex - count / 2; i <= list.PageIndex + (count - 1) / 2; i++)
                {
                    if (list.PageIndex == i)
                    {
                        sb.AppendLine("<li><a href='#' class='active' >" + (i + 1) + "</a></li>");
                        continue;
                    }
                    route["pageIndex"] = i;
                    sb.Append("<li>");
                    sb.AppendLine(helper.ActionLink(i + 1 + "", route["action"].ToString(), route).ToHtmlString());
                    sb.Append("</li>");
                }
                route["pageIndex"] = list.PageIndex == list.PageTotal - 1 ? list.PageTotal - 1 : list.PageIndex + 1;
                sb.Append("<li>");
                sb.AppendLine(helper.ActionLink(">", route["action"].ToString(), route).ToHtmlString());
                sb.Append("</li>");
                sb.AppendLine("</ul>");
                return new MvcHtmlString(sb.ToString());
            }
            else
            {
                for (int i = list.PageTotal - count; i < list.PageTotal; i++)
                {
                    if (list.PageIndex == i)
                    {
                        sb.AppendLine("<li><a href='#' class='active' >" + (i + 1) + "</a></li>");
                        continue;
                    }
                    route["pageIndex"] = i;
                    sb.Append("<li>");
                    sb.AppendLine(helper.ActionLink(i + 1 + "", route["action"].ToString(), route).ToHtmlString());
                    sb.Append("</li>");
                }
                route["pageIndex"] = list.PageIndex == list.PageTotal - 1 ? list.PageTotal - 1 : list.PageIndex + 1;
                sb.Append("<li>");
                sb.AppendLine(helper.ActionLink(">", route["action"].ToString(), route).ToHtmlString());
                sb.Append("</li>");
                sb.AppendLine("</ul>");
                return new MvcHtmlString(sb.ToString());
            }

        }

    }  
}

//控制器

 public class HomeController : Controller
    {
        private const int pageSize = 8;//一页显示pageSize条记录
        private  long  RecordTotal = 0; //总记录条数,从数据库中相应表中获取

      public ActionResult Index(int pageIndex=0)
        {//list是需要展示的某一页记录集合,即分页查询,对内存友好,但耗时间
             List<UserInfo>  list =
                       UserInfoDAL.GetUserByFenZhu<int>(pageIndex,pageSize,out RecordTotal,u=>u.Id).ToList<UserInfo>();
            PageOfList<UserInfo> List =
                      new PageOfList<UserInfo>(list,pageIndex,pageSize,RecordTotal);

            
            return View(List);
        }

}

 

//视图Index.cshtml

   <table>
            <tr><th>ID</th><th>用户名</th><th>密码</th></tr>
            @if (Model != null && Model.Count > 0)
            {
                foreach (var item in Model)
                {
                  <tr>
                     <td>@item.Id</td>
                      <td>@item.UName</td>
                      <td>@item.UPwd</td>
                  </tr>
                }
            }
        </table>

<!--分页导航栏Model是包装的部分实体集,7代表7个页码用户供选择-->

<!--可以做成公共视图-->
        @UI.Models.FenYeDaoHangLan.UIPaging(this.Html,Model,7)
   

使用MvcPager组件

使用教程网址:http://www.webdiyer.com/mvcpager/

//控制器·

   public class HomeController : Controller
    {
        private const int pageSize = 10;
        IQueryable<UserInfo> list = UserInfoDAL.GetUser(u => true).OrderBy(u => u.Id).AsQueryable();
        private long RecordTotal;
        public HomeController()
        {
            this.RecordTotal = this.list.Count();
        }
        public ActionResult Index(int pageIndex=1)
        {//this.list是表中的所有记录集合,耗内存,省时间
            PagedList<UserInfo> lst = PageLinqExtensions.ToPagedList(this.list,pageIndex,pageSize);
             return View(lst);
        }

    }

//视图


 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值