下载 MVcPager源代码以后,我们就先来修改一下那个分页使用的类PagedList。
PagedList<T> : List<T> PagedList(IList<T> items, pageIndex, PageSize = TotalItemCount = CurrentPageIndex = ( i = StartRecordIndex - ; i < EndRecordIndex; i++ PagedList(IEnumerable<T> 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<T> ToPagedList<T> ( this IEnumerable<T> 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<T>(pageOfItems, pageIndex, pageSize, totalItemCount); } }
3.在我们修改了扩展方法以后,就可以在IEnumerable以后进行.ToPagedList<T>转换成PagedList 分页类
将获取到的分页列表数据转换为PagedList类型以后,我们就要使用MvcPager来进行分页显示
我们定义了博客列表显示Controller,将从数据库中获取到的列表数据通过MvcPager分页
@{ Layout = null; } @using Webdiyer.WebControls.Mvc @model PagedList<Models.BlogInfo> @{ foreach (Models.BlogInfo blogInfo in Model) { <div class="post_item"> <div class="digg"> <div class="diggit"> <span class="diggnum">0</span> </div> <div class="clear"> </div> <div class="digg_tip"> </div> </div> <div class="post_item_body"> <h3> <a href="@blogInfo.LinkUrl" target="_blank" class="titlelink">@blogInfo.Title</a> </h3> <p class="post_iteme_summary"> @blogInfo.BlogContent ... </p> <div class="post_item_foot"> <a class="lightblue" href="@blogInfo.LinkUrl">@blogInfo.AuthorID</a> @blogInfo.PublishTime.ToString() <span class="article_comment"><a class="gray" title="">评论(0)</a> </span><span class="article_view"> <a class="gray">阅读(1)</a> </span> </div> </div> <div class="clear"> </div> </div> } @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.有图有证据 下面我来展示一下我的系统分页显示
转载于:https://blog.51cto.com/254240/1598521