使用HtmlHelper 写自己的 mvc 分页

最近项目中用到MVC3,很多分页,本人习惯自己自己去实现一些代码,看了很多控件,总感觉代码有点多,还要去应用他们的DLL。甚是繁琐。所以就自己实现一下。

定义分页信息类

 /// <summary>
    /// 分页信息
    /// </summary>
    public class PagingInfo
    {
        /// <summary>
        /// 显示连续页码数量
        /// </summary>
        public int ShowNum { get; set; }
        /// <summary>
        /// 记录总数
        /// </summary>
        public int TotalItems { get; set; }
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int ItemsPerPage { get; set; }
        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage { get; set; }
        /// <summary>
        /// 是否显示第一页和最后一页
        /// </summary>
        private bool _IsShowFirstLast = true;
        public bool IsShowFirstLast { get { return _IsShowFirstLast; } set { _IsShowFirstLast = value; } }
        public int TotalPages
        {
            get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
        }
    }
View Code

 

分页扩展

   /// <summary>
        /// 根据pageInfo 生成一个分页控件
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pageInfo"></param>
        /// <param name="pageUrl"></param>
        /// <returns></returns>
        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pageInfo, Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder(1000);
            bool isEven = (pageInfo.ShowNum & 1).Equals(0);
            int middleCeilingNum = (int)Math.Ceiling((decimal)pageInfo.ShowNum / 2);
            int middleFloorNum = (int)Math.Floor((decimal)pageInfo.ShowNum / 2);
            middleFloorNum = middleFloorNum == 0 ? 1 : middleFloorNum;
            int beginIndex = 1;//控件开始页数
            int endIndex = 1;//控件结束页数

            if (pageInfo.ShowNum >= pageInfo.TotalPages)
            {
                beginIndex = 1;
                endIndex = pageInfo.TotalPages;
            }
            else
            {
                if (!isEven)
                {
                    beginIndex = pageInfo.CurrentPage <= middleCeilingNum
                        ? 1
                        : pageInfo.CurrentPage - middleFloorNum;
                    endIndex = pageInfo.CurrentPage <= middleCeilingNum
                        ? pageInfo.ShowNum
                        : pageInfo.CurrentPage + middleFloorNum;
                    beginIndex = pageInfo.CurrentPage + middleCeilingNum > pageInfo.TotalPages
                        ? pageInfo.TotalPages - pageInfo.ShowNum + 1
                        : beginIndex;
                    endIndex = pageInfo.CurrentPage + middleCeilingNum > pageInfo.TotalPages
                        ? pageInfo.TotalPages
                        : endIndex;
                }
                else
                {
                    beginIndex = pageInfo.CurrentPage <= middleCeilingNum ? 1 : pageInfo.CurrentPage - middleFloorNum + 1;
                    endIndex = pageInfo.CurrentPage <= middleCeilingNum ? pageInfo.ShowNum : pageInfo.CurrentPage + middleFloorNum;

                    beginIndex = pageInfo.CurrentPage + middleCeilingNum > pageInfo.TotalPages
                        ? pageInfo.TotalPages - pageInfo.ShowNum + 1
                        : beginIndex;
                    endIndex = pageInfo.CurrentPage + middleCeilingNum > pageInfo.TotalPages
                        ? pageInfo.TotalPages
                        : endIndex;
                }
            }
            beginIndex = pageInfo.CurrentPage == pageInfo.ShowNum - middleFloorNum + 1
                ? beginIndex - 1 : beginIndex;
            beginIndex = pageInfo.CurrentPage == pageInfo.TotalPages + pageInfo.ShowNum - middleFloorNum
                    ? beginIndex + 1 : beginIndex;
            endIndex = pageInfo.CurrentPage == pageInfo.TotalPages - middleFloorNum - 1
                ? endIndex + 1 : endIndex;
            if (pageInfo.TotalPages <= pageInfo.ShowNum + 2)
            {
                result.Append(GetMiddle(pageInfo, beginIndex, endIndex, pageUrl));
            }
            else
            {
                //前一页
                if (pageInfo.CurrentPage != 1)
                    result.Append(GetUrl(pageInfo, pageUrl, pageInfo.CurrentPage - 1, "上一页").ToString());
                //第一页
                if (pageInfo.CurrentPage > middleCeilingNum + 1 && pageInfo.IsShowFirstLast)
                    result.Append(GetUrl(pageInfo, pageUrl, 1, "1").ToString());
                if (pageInfo.CurrentPage > middleCeilingNum && pageInfo.CurrentPage != pageInfo.ShowNum - middleFloorNum + 1)
                    result.Append(GetSplitStr().ToString());
                result.Append(GetMiddle(pageInfo, beginIndex, endIndex, pageUrl));

                if (pageInfo.CurrentPage < pageInfo.TotalPages - middleFloorNum && pageInfo.CurrentPage != pageInfo.TotalPages - middleFloorNum - 1)
                    result.Append(GetSplitStr().ToString());

                //最后一页
                if (
                    pageInfo.CurrentPage < pageInfo.TotalPages - (middleFloorNum + 1) && pageInfo.IsShowFirstLast)
                    result.Append(GetUrl(pageInfo, pageUrl, pageInfo.TotalPages, pageInfo.TotalPages + "").ToString());
                //下一页
                if (pageInfo.TotalPages != 1 && (pageInfo.CurrentPage != pageInfo.TotalPages))
                    result.Append(GetUrl(pageInfo, pageUrl, pageInfo.CurrentPage + 1, "下一页").ToString());
            }
            return MvcHtmlString.Create(result.ToString());
        }
        /// <summary>
        /// 特殊页面(第一页,前一页,后一页,最后一页)的连接生成
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <param name="pageUrl"></param>
        /// <param name="i"></param>
        /// <param name="ShowTag">显示文字</param>
        /// <returns></returns>
        private static TagBuilder GetUrl(PagingInfo pageInfo, Func<int, string> pageUrl, int i, string ShowTag)
        {
            TagBuilder tag = new TagBuilder("a");
            tag.MergeAttribute("href", pageUrl(i));
            tag.InnerHtml = ShowTag;
            return tag;
        }

        private static TagBuilder GetSplitStr()
        {
            TagBuilder tag = new TagBuilder("span");
            tag.MergeAttribute("class", "psplit");
            tag.InnerHtml = "...";
            return tag;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static string GetMiddle(PagingInfo pageInfo, int beginIndex, int endIndex, Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder(300);
            for (int i = beginIndex; i <= endIndex; i++)
            {
                TagBuilder tag = new TagBuilder("a");
                tag.MergeAttribute("href", pageUrl(i));
                tag.InnerHtml = i.ToString();
                if (i == pageInfo.CurrentPage)
                    tag.AddCssClass("selected");
                result.Append(tag.ToString());
            }
            return result.ToStr();
        }
View Code

 

定义一个model

public class PhotoAlbumReply : IReply
    {
        public string Id { get; set; }
        public string Content { get; set; }
    }
View Code

  

定义一个viewmodel

 public class PhotoAlbumReplyViewModel
    {
        public IEnumerable<PhotoAlbumReply> PhotoAlbumReplys { get; set; }
        public PagingInfo PagingInfo { get; set; }
    }
View Code

 

写自己的controller

public ActionResult List(int page)
        {
            int pageSize = 20;
            int count = 100;//这个自己去实现
            IEnumerable<PhotoAlbumReply> PhotoAlbumReplys = null;//这个自己去实现
            return View(
                new PhotoAlbumReplyViewModel()
                {
                    PhotoAlbumReplys = PhotoAlbumReplys,
                    PagingInfo = new PagingInfo()
                    {
                        IsShowFirstLast = true,
                        ShowNum = 4,
                        CurrentPage = page,
                        ItemsPerPage = pageSize,
                        TotalItems = count
                    }

                });
        }
View Code

 

view 调用

@{
    ViewBag.Title = "List";
}
<style type="text/css">
    .pager a {padding: 10px;}
    .pager .selected {padding: 10px;color: red;} 
</style>
@model ViewModel.PhotoAlbumReplyViewModel
 @if (null != Model.PhotoAlbumReplys && Model.PhotoAlbumReplys.Count() > 0)
    {
<table border="1">
    <tr>
        <td>ID</td>
        <td>content</td>
    </tr>
        foreach (var item in Model.PhotoAlbumReplys)
        {
        <tr>
            <td>@item.Id</td>
            <td>@item.TargetId</td>
            <td>@item.Content</td>  
        </tr>
        }    
   </table>
} 
<div class="pager"> @Html.PageLinks(Model.PagingInfo, x => Url.Action("List", "Photo", new {page=x})) </div>
View Code

打完收工。 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。