下载 MVcPager源代码以后,我们就先来修改一下那个分页使用的类PagedList。

   PagedList
 : List
           PagedList(IList
 items,  pageIndex,              PageSize =            TotalItemCount =            CurrentPageIndex =             ( i = StartRecordIndex - ; i < EndRecordIndex; i++             PagedList(IEnumerable
 items,  pageIndex,  pageSize,  TotalItemCount =           CurrentPageIndex =ageSize =           CurrentPageIndex { ;           PageSize { ;   TotalItemCount { ;   TotalPageCount {  {  ()Math.Ceiling(TotalItemCount / (         StartRecordIndex {  {  (CurrentPageIndex - ) * PageSize +           EndRecordIndex {  {  TotalItemCount > CurrentPageIndex * PageSize ? CurrentPageIndex *   }

2.在我们修改了PagedList类以后,还要修改哪些地方呢?还有一个扩展方法,用来扩展PagedList

  public static class PageLinqExtensions        {           public static PagedList
 ToPagedList
 (                      this IEnumerable
 allItems,  int pageIndex, int pageSize )               {                 if (pageIndex < 1)                         pageIndex = 1;                    var itemIndex = (pageIndex-1) * pageSize;                               var pageOfItems = allItems.Skip(itemIndex).Take(pageSize);                              var totalItemCount = allItems.Count();                                          return new PagedList
(pageOfItems, pageIndex, pageSize, totalItemCount);                                       }           }

3.在我们修改了扩展方法以后,就可以在IEnumerable以后进行.ToPagedList<T>转换成PagedList 分页类

将获取到的分页列表数据转换为PagedList类型以后,我们就要使用MvcPager来进行分页显示

我们定义了博客列表显示Controller,将从数据库中获取到的列表数据通过MvcPager分页

 @{     Layout = null;  }   @using Webdiyer.WebControls.Mvc   @model PagedList
   @{     foreach (Models.BlogInfo blogInfo in Model)          {     
           
              
                               
0                    
                            
                                                                             
           

                
@blogInfo.Title          

                                       
                 @blogInfo.BlogContent ...           

                                                   
                
@blogInfo.AuthorID                @blogInfo.PublishTime.ToString()                      
评论(0) 
                    
阅读(1)                                     
                  }                                 @Html.AjaxPager(Model, new PagerOptions()                     {                          PageIndexParameterName = "id",               ShowDisabledPagerItems = false,                        ShowPageIndexBox = true               },                              new AjaxOptions() { UpdateTargetId = "post_list", OnBegin = "function(){alert('Ajax OnBegin 事件被引发');}", OnComplete = "AjaxStop" })                }

5.Html.AjaxPager()方法就是MvcPager的一个扩展方法,扩展了HtmlHelper。对于其中的几个参数我就不介绍了,也很容易理解。

6.有图有证据 下面我来展示一下我的系统分页显示