• 【DNN 系列】 MVC 分页


    MVC分页可采用插件形式, 有MvcPage那个插件但是我觉得那个是假分页 有点影响效率 所以网上找了一个例子来

    做分页

    1,

    PagerQuery.cs

    public class PagerQuery<TPager, TEntityList>
        {
            public PagerQuery(TPager pager, TEntityList entityList)
            {
                this.Pager = pager;
                this.EntityList = entityList;
            }
            public TPager Pager { get; set; }
            public TEntityList EntityList { get; set; }
      }

    PagerInfo

    public class PagerInfo
        {
            public int RecordCount { get; set; }
    
            public int CurrentPageIndex { get; set; }
    
            public int PageSize { get; set; }
    
    
    
        }

    PagerHelper

    namespace DotNetNuke.Web.Mvc.Helpers
    {
        public static class PagerHelper
        {
            /// <summary>
            /// 分页
            /// </summary>
            /// <param name="helper"></param>
            /// <param name="id">分页id</param>
            /// <param name="currentPageIndex">当前页</param>
            /// <param name="pageSize">分页尺寸</param>
            /// <param name="recordCount">记录总数</param>
            /// <param name="htmlAttributes">分页头标签属性</param>
            /// <param name="className">分页样式</param>
            /// <param name="mode">分页模式</param>
            /// <returns></returns>
            public static string Pager(this DnnHtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className, PageMode mode, string url)
            {
                TagBuilder builder = new TagBuilder("table");
                builder.IdAttributeDotReplacement = "_";
                builder.GenerateId(id);
                builder.AddCssClass(className);
                builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
                builder.InnerHtml = GetNormalPage(currentPageIndex, pageSize, recordCount, mode, url);
                return builder.ToString();
            }
            /// <summary>
            /// 分页
            /// </summary>
            /// <param name="helper"></param>
            /// <param name="id">分页id</param>
            /// <param name="currentPageIndex">当前页</param>
            /// <param name="pageSize">分页尺寸</param>
            /// <param name="recordCount">记录总数</param>
            /// <param name="className">分页样式</param>
            /// <returns></returns>
            public static string Pager(this DnnHtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className,string url)
            {
                return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className, PageMode.Normal,url);
            }
            /// <summary>
            /// 分页
            /// </summary>
            /// <param name="helper"></param>
            /// <param name="id">分页id</param>
            /// <param name="currentPageIndex">当前页</param>
            /// <param name="pageSize">分页尺寸</param>
            /// <param name="recordCount">记录总数</param>
            /// <returns></returns>
            public static string Pager(this DnnHtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string url)
            {
                return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, url);
            }
            /// <summary>
            /// 分页
            /// </summary>
            /// <param name="helper"></param>
            /// <param name="id">分页id</param>
            /// <param name="currentPageIndex">当前页</param>
            /// <param name="pageSize">分页尺寸</param>
            /// <param name="recordCount">记录总数</param>
            /// <param name="mode">分页模式</param>
            /// <returns></returns>
            public static string Pager(this DnnHtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, PageMode mode, string url)
            {
                return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, mode, url);
            }
            /// <summary>
            /// 分页
            /// </summary>
            /// <param name="helper"></param>
            /// <param name="id">分页id</param>
            /// <param name="currentPageIndex">当前页</param>
            /// <param name="pageSize">分页尺寸</param>
            /// <param name="recordCount">记录总数</param>
            /// <param name="className">分页样式</param>
            /// <param name="mode">分页模式</param>
            /// <returns></returns>
            public static string Pager(this DnnHtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className, PageMode mode, string url)
            {
                return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className, mode, url);
            }
            /// <summary>
            /// 获取普通分页
            /// </summary>
            /// <param name="currentPageIndex"></param>
            /// <param name="pageSize"></param>
            /// <param name="recordCount"></param>
            /// <returns></returns>
            private static string GetNormalPage(int currentPageIndex, int pageSize, int recordCount, PageMode mode, string url)
            {
                int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1);
    
                UrlHelper U = new UrlHelper();
                url = url + "?page={0}";
                NameValueCollection collection = HttpContext.Current.Request.QueryString;
                string[] keys = collection.AllKeys;
                for (int i = 0; i < keys.Length; i++)
                {
                    if (keys[i].ToLower() != "page")
                        url = url + string.Format("&{0}={1}", keys[i], collection[keys[i]]);
                }
    
                StringBuilder sb = new StringBuilder();
                sb.Append("<tr><td>");
                sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页&nbsp;&nbsp;", recordCount, pageCount, currentPageIndex);
                if (currentPageIndex == 1)
                    sb.Append("<span>首页</span>&nbsp;");
                else
                {
                    string url1 = string.Format(url.ToString(), 1);
                    sb.AppendFormat("<span><a href={0}>首页</a></span>&nbsp;", url1);
                }
                if (currentPageIndex > 1)
                {
                    string url1 = string.Format(url.ToString(), currentPageIndex - 1);
                    sb.AppendFormat("<span><a href={0}>上一页</a></span>&nbsp;", url1);
                }
                else
                    sb.Append("<span>上一页</span>&nbsp;");
                if (mode == PageMode.Numeric)
                    sb.Append(GetNumericPage(currentPageIndex, pageSize, recordCount, pageCount, url.ToString()));
                if (currentPageIndex < pageCount)
                {
                    string url1 = string.Format(url.ToString(), currentPageIndex + 1);
                    sb.AppendFormat("<span><a href={0}>下一页</a></span>&nbsp;", url1);
                }
                else
                    sb.Append("<span>下一页</span>&nbsp;");
    
                if (currentPageIndex == pageCount)
                    sb.Append("<span>末页</span>&nbsp;");
                else
                {
                    string url1 = string.Format(url.ToString(), pageCount);
                    sb.AppendFormat("<span><a href={0}>末页</a></span>&nbsp;", url1);
                }
                return sb.ToString();
            }
            /// <summary>
            /// 获取数字分页
            /// </summary>
            /// <param name="currentPageIndex"></param>
            /// <param name="pageSize"></param>
            /// <param name="recordCount"></param>
            /// <param name="pageCount"></param>
            /// <param name="url"></param>
            /// <returns></returns>
            private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount, string url)
            {
                int k = currentPageIndex / 10;
                int m = currentPageIndex % 10;
                StringBuilder sb = new StringBuilder();
                if (currentPageIndex / 10 == pageCount / 10)
                {
                    if (m == 0)
                    {
                        k--;
                        m = 10;
                    }
                    else
                        m = pageCount % 10;
                }
                else
                    m = 10;
                for (int i = k * 10 + 1; i <= k * 10 + m; i++)
                {
                    if (i == currentPageIndex)
                        sb.AppendFormat("<span><font color=red><b>{0}</b></font></span>&nbsp;", i);
                    else
                    {
                        string url1 = string.Format(url.ToString(), i);
                        sb.AppendFormat("<span><a href={0}>{1}</a></span>&nbsp;", url1, i);
                    }
                }
    
                return sb.ToString();
            }
        }
        /// <summary>
        /// 分页模式
        /// </summary>
        public enum PageMode
        {
            /// <summary>
            /// 普通分页模式
            /// </summary>
            Normal,
            /// <summary>
            /// 普通分页加数字分页
            /// </summary>
            Numeric
        }
    }

    View

    @inherits DotNetNuke.Web.Mvc.Framework.DnnWebViewPage<Dnn.Modules.PagerQuery<Dnn.Modules.PagerInfo, IEnumerable<Dnn.Modules.Models.BasicDicMain>>>
    
    
    
    <p>
                @Html.Raw(Html.Pager("pager", Model.Pager.CurrentPageIndex, Model.Pager.PageSize, Model.Pager.RecordCount, PageMode.Numeric, Url.Action("Index", "BasicDicMain")))
     </p>

    Controller

    PagerInfo pager = new PagerInfo();
                BasicDicMain info = new BasicDicMain();
                info = new BasicDicMain();
                pager.RecordCount = BasicDicMainManager.Instance.GetBasicDicMain().Count();
                pager.PageSize = 10;
                pager.CurrentPageIndex = (page != null ? (int)page : 1);
                IEnumerable<BasicDicMain> result = BasicDicMainManager.Instance.GetPageBasicDicMain(ModuleContext.ModuleId, pager.CurrentPageIndex, pager.PageSize);
                PagerQuery<PagerInfo, IEnumerable<BasicDicMain>> query = new PagerQuery<PagerInfo, IEnumerable<BasicDicMain>>(pager, result);
                return View(query);

    名称:多罗贝勒
    博客地址:http://www.cnblogs.com/objctccc/
    欢迎转载

  • 相关阅读:
    云计算openstack核心组件——neutron网络服务(9)
    云计算openstack核心组件——neutron网络服务(8)
    flexible.js 布局详解
    libflexible源码阅读
    H5适配方案
    微信分享朋友圈示例代码
    微信接口示例代码
    微信JSSDK上传图片,代码为上传单张图
    H5动画开发快车道
    Centos 6.5 安装 Nginx+MySQL+PHP
  • 原文地址:https://www.cnblogs.com/objctccc/p/5867237.html
Copyright © 2020-2023  润新知