zoukankan      html  css  js  c++  java
  • 关于Mvc的分页写法

    关于asp.net mvc的分页,网上已经有很多了。本来也想借用,先看了杨涛写的分页控件,感觉用起来稍微有点复杂,而我只需要简单的分页。分页我写过很多次,原理也熟悉,就是构造首页、上一页、下一页及末页的链接,做得好点,还可以有页码、下拉分页等。于是我又造了一个轮子。

    先准备数据,这里以人员信息为例:

    public class PersonInfo
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

    初始化100条数据,并提供一个方法,可以从这些数据中按照分页大小和页码获取。

    public class PersonHelper
    {
        private static List<PersonInfo> list;
        static PersonHelper()
        {
            list = new List<PersonInfo>();
            for (int i = 0; i < 100; i++)
            {
                list.Add(new PersonInfo()
                {
                    Name = "姓名" + i.ToString(),
                    Age = 18 + i
                });
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<PersonInfo> GetList(int pageSize, int pageIndex)
        {
            return list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }
    }

    Model定义:其中包含了分页大小、当前页码、记录数和人员信息集合。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using MvcApplication2.Code;
      
    namespace MvcApplication2.Models
    {
        public class PersonListModels
        {
            public int PageIndex { get; set; }
            public int PageSize { set; get; }
            public int RecordCount { get; set; }
            public IEnumerable<PersonInfo> Persons { get; set; }
        }
    }

    Controller中的处理:

    public ActionResult PersonList(int? pageIndex)
            {
                // 分页大小
                int pageSize = 10;
      
                // 获取分页页码
                if (pageIndex == null || pageIndex <= 0)
                {
                    pageIndex = 1;
                }
      
                // 获取分页数据
                IEnumerable<PersonInfo> query = PersonHelper.GetList(pageSize, pageIndex.Value);
      
                // 设置模型
                NewsModels model = new NewsModels()
                {
                    Persons = query,
                    PageIndex = pageIndex.Value,
                    PageSize = pageSize,
                    RecordCount = 100
                };
      
                return View(model);
            }

    View中处理:

    @model MvcApplication2.Models.PersonListModels
    @{
        ViewBag.Title = "Person List";
    }
    <h2>
        Person List</h2>
    <table>
        @foreach (MvcApplication2.Code.PersonInfo info in Model.Persons)
        {
            <tr>
                <td>@info.Name
                </td>
                <td>@info.Age
                </td>
            </tr>
        }
        <tr>
            <td colspan="2">
                @Url.Pager("Home", "PersonList", Model.PageSize, Model.PageIndex, Model.RecordCount)
            </td>
        </tr>
    </table>

    重点就在@Url.Pager的使用了。扩展UrlHelper的代码如下:

    namespace System.Web.Mvc
    {
        public static class HtmlExtend
        {
            /// <summary>
            /// 扩展UrlHelper,实现输出分页HTML
            /// </summary>
            /// <param name="urlHelper"></param>
            /// <param name="controllerName">控制器名</param>
            /// <param name="actionName">行为名</param>
            /// <param name="pageSize">分页大小</param>
            /// <param name="pageIndex">当前页码</param>
            /// <param name="recordCount">总记录数</param>
            /// <returns></returns>
            public static MvcHtmlString Pager(this UrlHelper urlHelper, string controllerName, string actionName, int pageSize, int pageIndex, int recordCount)
            {
                // 如果分页大小等于0,则返回空字符串
                if (pageSize == 0)
                {
                    return MvcHtmlString.Create(string.Empty);
                }
      
                // 根据总记录数和分页大小计算出分页数量
                int pageCount = (int)decimal.Ceiling((decimal)recordCount / (decimal)pageSize);
      
                // 首页、末页
                string firstStr = string.Empty;
                string lastStr = string.Empty;
                if (recordCount > 0)
                {
                    string firstUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = 1 });
                    firstStr = "<a href='" + firstUrl + "'>首页</a>";
      
                    string lastUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = pageCount });
                    lastStr = "<a href='" + lastUrl + "'>末页</a>";
                }
                else
                {
                    firstStr = "首页";
                    lastStr = "末页";
                }
      
                // 上一页
                string preStr = string.Empty;
                if (pageIndex > 1 && pageIndex <= pageCount)
                {
                    string prevUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = pageIndex - 1 });
                    preStr = "<a href='" + prevUrl + "'>上一页</a>";
                }
                else
                {
                    preStr = "上一页";
                }
      
                // 下一页
                string nextStr = string.Empty;
                if (pageIndex > 0 && pageIndex < pageCount)
                {
                    string nextUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = pageIndex + 1 });
                    nextStr = "<a href='" + nextUrl + "'>下一页</a>";
                }
                else
                {
                    nextStr = "下一页";
                }
      
                // 页码
                string numStr = string.Empty;
                if (pageCount > 0)
                {
                    // 遍历输出全部的页码
                    for (int i = 1; i <= pageCount; i++)
                    {
                        string numUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = i });
      
                        // 当前页码加粗
                        if (i == pageIndex)
                        {
                            numStr += "[<a href='" + numUrl + "'><strong>" + i + "</strong></a>] ";
                        }
                        else
                        {
                            numStr += "[<a href='" + numUrl + "'>" + i + "</a>] ";
                        }
                    }
                }
      
                string pageStr = firstStr + " " + preStr + " " + numStr + nextStr + " " + lastStr;
      
                return MvcHtmlString.Create(pageStr);
            }
        }
    }

    看看效果:

    aspnetmvcpager 发布一个ASP.NET MVC的分页控件(扩展UrlHelper)

    这个扩展没有实现页码分段显示,有兴趣的朋友可以自己试试。

    文章来源:http://blog.bossma.cn/asp_net_mvc/asp-net-mvc-url-pager/

  • 相关阅读:
    ckplayer-超酷网页视频播放器的使用
    我的第一篇博文(Winfrom下WebBrowser控件的使用)
    estore商城案例(四、五)------添加购物车&购物车显示/生成订单&在线支付
    estore商城案例(三)------Filter过滤器:自动登录&权限管理
    estore商城案例(二)------登录&添加商品&商品列表(下)
    estore商城案例(二)------登录&添加商品&商品列表(上)
    myeclipse2014在线安装aptana
    estore商城案例(一)------用户注册&邮件激活(下)
    estore商城案例(一)------用户注册&邮件激活(上)
    使用指南
  • 原文地址:https://www.cnblogs.com/yingger/p/3407642.html
Copyright © 2011-2022 走看看