博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mvc3分页封装
阅读量:5278 次
发布时间:2019-06-14

本文共 6055 字,大约阅读时间需要 20 分钟。

1.先自定义一个类为PagingInfo

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace eBuul.ChinaJoy.BLL.Models

{
    public class PagingInfo
    {
        /// <summary>
        /// 总条数
        /// </summary>
        public int TotalItems { get; set; }
        /// <summary>
        /// 每页条数
        /// </summary>
        public int ItemsPerPage { get; set; }
        /// <summary>
        /// 当前页数
        /// </summary>
        public int CurrentPage { get; set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages
        {
            get
            {
                if (ItemsPerPage!=0)
                {
                    return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); 
                }
                return 0;
               
              
            }
        }
    }
}

2.在Action中写入以下代码:

PagingInfo pagingInfo = new PagingInfo() { CurrentPage = pageIndex, ItemsPerPage = pageSize, TotalItems = total };

ViewBag.PagingInfo = pagingInfo;

3.在view视图中调用

  <div class="pages">

            @Html.PageLinkes("FineGame", "Index", (eBuul.ChinaJoy.BLL.Models.PagingInfo)ViewBag.PagingInfo, new string[] { "Keyword" })
        </div>

4.Html扩展方法实现

 

 

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using eBuul.ChinaJoy.BLL.Models;

namespace System.Web.Mvc.Html
{
    public static class PagingHelpers
    {
        public static MvcHtmlString PageLinkes(this HtmlHelper html, string controller, string ActionName, PagingInfo pagingInfo, string[] paramArray)
        {
            HttpContext contenxt = HttpContext.Current;
            if (pagingInfo.TotalItems==0)
            {
                return MvcHtmlString.Create("");
            }
            //设置分页显示的区间值
            int IsNextPage = 1;
            int currentPage = (pagingInfo.CurrentPage - 1) / 5;
            int start = 1, end = 5;
            if (currentPage >= 1)
            {
                start = (currentPage * 5) + 1;
                end = (currentPage + 1) * 5;
            }
            StringBuilder result = new StringBuilder();
            string tmp = "";
            if (paramArray != null && paramArray.Length > 0)
            {
                for (int n = 0; n < paramArray.Length; n++)
                {
                    if (contenxt.Request.Params.AllKeys.Contains(paramArray[n]))
                        tmp += "&" + paramArray[n] + "=" + contenxt.Request.Params[paramArray[n]];
                }
            }

            //生成中间的分页数目

            int i = 0;
            for (i = start; i <= end; i++)
            {
                if (i <= pagingInfo.TotalPages)
                {
                    TagBuilder litag = new TagBuilder("li");
                    TagBuilder tag = new TagBuilder("a");
                    if (tmp != "")
                        tag.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, i, tmp));
                    else
                        tag.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, i));
                    tag.InnerHtml = i.ToString();
                    if (i == pagingInfo.CurrentPage)
                        litag.AddCssClass("li_bg");
                    litag.InnerHtml = tag.ToString();
                    result.Append(litag.ToString());
                    IsNextPage++;
                }
                else
                    break;
            }
            //设置分页显示的下一区间值的起始位置(即页面中的"..."按钮)
            if (pagingInfo.TotalPages > 5 && (i - 1) != pagingInfo.TotalPages)
            {
                TagBuilder litagDot = new TagBuilder("li");
                TagBuilder tagDot = new TagBuilder("a");
                if (tmp != "")
                    tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, end + 1, tmp));
                else
                    tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, end + 1));
                tagDot.InnerHtml = "...";
                litagDot.InnerHtml = tagDot.ToString();
                result.Append(litagDot.ToString());
            }
            //设置分页显示的上一区间值的起始位置(即页面中的"..."按钮)
            if (currentPage >= 1)
            {
                TagBuilder litagDot = new TagBuilder("li");
                TagBuilder tagDot = new TagBuilder("a");
                if (tmp != "")
                    tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, start - 1, tmp));
                else
                    tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, start - 1));
                tagDot.InnerHtml = "...";
                litagDot.InnerHtml = tagDot.ToString();
                result.Insert(0, litagDot.ToString());
            }

            //上一页

            if (pagingInfo.TotalPages > 1)
            {
                TagBuilder litagPre = new TagBuilder("li");
                TagBuilder tagPre = new TagBuilder("a");
                //TagBuilder tagImgPre = new TagBuilder("img");
                //tagImgPre.MergeAttribute("src", "/Content/imghome/left02.png");

                if (pagingInfo.CurrentPage == 1)

                    tagPre.MergeAttribute("href", "#");
                else
                {
                    if (tmp != "")
                        tagPre.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, pagingInfo.CurrentPage - 1, tmp));
                    else
                        tagPre.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, pagingInfo.CurrentPage - 1));
                }
                //tagPre.InnerHtml = tagImgPre.ToString();
             
                    tagPre.InnerHtml = "上一页";
                litagPre.InnerHtml = tagPre.ToString();
                result.Insert(0, litagPre);
            }
            //下一页
            if (pagingInfo.TotalPages > 1)
            {
                TagBuilder litagNext = new TagBuilder("li");
                TagBuilder tagNext = new TagBuilder("a");
                //TagBuilder tagImgNext = new TagBuilder("img");
                //tagImgNext.MergeAttribute("src", "/Content/imghome/right02.png");
                if (pagingInfo.CurrentPage == pagingInfo.TotalPages)
                    tagNext.MergeAttribute("href", "#");
                else
                {
                    if (tmp != "")
                        tagNext.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, pagingInfo.CurrentPage + 1, tmp));
                    else
                        tagNext.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, pagingInfo.CurrentPage + 1));
                }
                //tagNext.InnerHtml = tagImgNext.ToString();
            
                
                    tagNext.InnerHtml = "下一页";
            
                litagNext.InnerHtml = tagNext.ToString();
                result.Append(litagNext.ToString());
            }
            //总页数
            TagBuilder litagTotal = new TagBuilder("li");
            TagBuilder spantag = new TagBuilder("span");
            litagTotal.AddCssClass("li_none");
            spantag.InnerHtml = pagingInfo.TotalItems.ToString();
            string paramUrl =  string.Format("/{0}/{1}{2}", controller, ActionName, string.IsNullOrWhiteSpace(tmp)?string.Empty:"?"+tmp.TrimStart('&'));
            string pageHtml = string.Format("共<span id=\"pageSum\">{0}</span>页&nbsp;&nbsp;{1}条记录&nbsp;到第<input type=\"text\" size=\"2\" id=\"jumpto\" name=\"jumpto\" class=\"inputpage\" title=\"指定页码\" value=\"{3}\">页<input type=\"button\" title=\"指定页码\" value=\"确定\" id=\"btnGO\" class=\"button_li\"><input type=\"hidden\" value=\"{2}\" id=\"hdnParamUrl\"", pagingInfo.TotalPages, spantag.ToString(), paramUrl, pagingInfo.TotalPages == pagingInfo.CurrentPage ? pagingInfo.CurrentPage : pagingInfo.CurrentPage+1);
           litagTotal.InnerHtml = pageHtml;
            result.Append(litagTotal.ToString());

            TagBuilder ultag = new TagBuilder("ul");

            ultag.InnerHtml = result.ToString();
            return MvcHtmlString.Create(ultag.ToString());
        }
    }
}

转载于:https://www.cnblogs.com/yxlblogs/archive/2013/04/24/3040737.html

你可能感兴趣的文章
Anaconda多环境多版本python配置指导
查看>>
测试与封装5.2-5.3
查看>>
WEB应用的组成结构
查看>>
bzoj3504[Cqoi2014]危桥
查看>>
Pomelo.AspNetCore.TimedJob 允许依赖注入
查看>>
记一次Spring的aop代理Mybatis的DAO所遇到的问题
查看>>
20165231 预备作业二:学习基础和C语言基础调查
查看>>
Unable to correct problems, you have held broken package
查看>>
文件操作
查看>>
Java并发编程——线程安全及解决机制简介
查看>>
day06_05 字典
查看>>
对一个同事项目的思考和总结
查看>>
【原】测试Application.Idle
查看>>
51nod 1107(树状数组、逆序数)
查看>>
二叉树的顺序存储结构
查看>>
“文件系统”与“根文件系统”详解
查看>>
基于S3C2440的U-BOOT的start.S分析
查看>>
[时间线]Android市场的发展
查看>>
软件开发的 “三重门”
查看>>
深入理解软件包的配置、编译与安装
查看>>