基于Bootstrap的Asp.net Mvc 分页的实现

最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下。首先新建一个Mvc 项目,既然是分页就需要一些数据,我这边是模拟了一些假的数据,实际的项目中都是在数据库中去取得的,很简单的数据:

1  public class User
2 {
3 public string Name { get; set; }
4
5 public int Age { get; set; }
6 }

取数据我这边是加了120个数据:

 1   public List<User> GetData()
2 {
3 List<User> list = new List<User>();
4 string[] array = new string[] { "Tom", "Joy", "James", "Kobe", "Jodan", "LiLei", "Hanmeimei", "xiaoming", "Danneil", "Forest", "Newbee", "Azure" };
5 for (int i = 0; i < 120; i++)
6 {
7 User user = new User();
8 user.Age = i;
9 user.Name = array[i / 10];
10 list.Add(user);
11 }
12
13
14 return list;
15 }

然后新建一个 PageModel类

 1 /// <summary>
2 /// 有些属性我写成了虚的, 这样可以根据不同的需要去重写便于扩展
3 /// </summary>
4 public class BasePageModel
5 {
6 public string SearchKeyWord { get; set; }
7
8 /// <summary>
9 ///点击分页是指向 Action 的名字 根据具体需要而定
10 /// </summary>
11 public virtual string ActionName
12 {
13 get
14 {
15 return "Index";
16 }
17 }
18
19 public int TotalCount { get; set; }
20
21 public int CurrentIndex { get; set; }
22
23 public int TotalPages
24 {
25 get
26 {
27 return (int)Math.Ceiling((double)TotalCount / (double)PageSize);
28 }
29 }
30
31 /// <summary>
32 /// 根据需要具体而定PageSize
33 /// </summary>
34 public virtual int PageSize
35 {
36 get { return 10; }
37 }
38
39 /// <summary>
40 ///根据需要具体而定 分页显示最大的页数
41 /// </summary>
42 public virtual int DisplayMaxPages
43 {
44 get
45 {
46 return 10;
47 }
48 }
49
50 public bool IsHasPrePage
51 {
52 get
53 {
54 return CurrentIndex != 1;
55 }
56 }
57
58 public bool IsHasNextPage
59 {
60 get
61 {
62 return CurrentIndex != TotalPages;
63 }
64 }
65 }

再新建一个分布式图 建在Shared 文件夹里,代码如下:

 1 @using MvcTest.Models
2 @model MvcTest.Models.BasePageModel
3
4 @{if (Model != null && Model.TotalPages != 0)
5 {
6 <ul class="pagination">
7 @{
8
9 @Url.CreatPageLiTag(Model, Model.CurrentIndex - 1, false, Model.IsHasPrePage, "&laquo;")
10
11 if (Model.TotalPages <= Model.DisplayMaxPages)
12 {
13 for (int i = 1; i < Model.TotalPages; i++)
14 {
15 @Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);
16 }
17 }
18 else
19 {
20 if (Model.CurrentIndex - 1 < 5)
21 {
22 for (int i = 1; i <= Model.DisplayMaxPages - 1; i++)
23 {
24 @Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);
25 }
26
27 @Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");
28 }
29 else
30 {
31 @Url.CreatPageLiTag(Model, 1);
32
33
34 if (Model.CurrentIndex + (Model.DisplayMaxPages - 2) / 2 >= Model.TotalPages)
35 {
36 int page = Model.CurrentIndex - (Model.DisplayMaxPages - Model.TotalPages + Model.CurrentIndex - 1);
37
38 if (page > 1)
39 {
40 @Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");
41 }
42
43 for (int i = page + 1; i < Model.TotalPages; i++)
44 {
45 @Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);
46 }
47 }
48 else
49 {
50 int page = Model.CurrentIndex - (Model.DisplayMaxPages - 2) / 2;
51
52 if (page > 2)
53 {
54 @Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");
55 }
56
57 for (int i = page; i < Model.CurrentIndex + (Model.DisplayMaxPages - 2) / 2; i++)
58 {
59 @Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);
60 }
61 @Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");
62 }
63
64 }
65 }
66
67 @Url.CreatPageLiTag(Model, Model.TotalPages, Model.TotalPages == Model.CurrentIndex)
68 @Url.CreatPageLiTag(Model, Model.CurrentIndex + 1, false, Model.IsHasNextPage, "&raquo;")
69
70 }
71 </ul>
72
73 }}

以上就是分页的核心代码,包括了一些判断逻辑,其中的 @Url.CreatPageLiTag 我是写了一个扩展

 1 public static class HtmlHelperExtensions
2 {
3 public static MvcHtmlString CreatPageLiTag(this UrlHelper urlHelper,
4 BasePageModel pageModel,
5 int index,
6 bool isCurrentIndex = false,
7 bool isDisable = true,
8 string content = "")
9 {
10
11 string url = urlHelper.Action(pageModel.ActionName, new { searchkey = pageModel.SearchKeyWord, index = index });
12 string activeClass = !isCurrentIndex ? string.Empty : "class='active'";
13 string disableClass = isDisable ? string.Empty : "class='disabled'";
14 url = isDisable ? "href='" + url + "'" : string.Empty;
15 string contentString = string.IsNullOrEmpty(content) ? index.ToString() : content;
16
17 return new MvcHtmlString("<li " + activeClass + disableClass + "><a " + url + ">" + contentString + "</a></li>");
18 }
19 }

在这里面里面 是生成<a/>标签的,样式可以自己定。无非就是一些css 的定义。

然后就在action 的方法里取数据

 1   public ActionResult Index(string searchkey, string index)
2 {
3 if (string.IsNullOrEmpty(index))
4 index = "1";
5 if (string.IsNullOrEmpty(searchkey))
6 searchkey = string.Empty;
7
8 List<User> totalList = GetData().Where(p=>p.Name.ToLower().Contains(searchkey.ToLower())).ToList();
9 BasePageModel page = new BasePageModel() { SearchKeyWord = searchkey, CurrentIndex = Int32.Parse(index), TotalCount = totalList.Count };
10
11 List<User> pageList = totalList.Skip((page.CurrentIndex - 1) * page.PageSize).Take(page.PageSize).ToList();
12 ViewData["pagemodel"] = page;
13 return View(pageList);
14 }

前台代码:

 1 @model List<MvcTest.Controllers.User>
2 @{
3 ViewBag.Title = "Index";
4 }
5
6 <h2>Data List</h2>
7 <form class="navbar-form navbar-right" name="searchform" action="@Url.Action("Index", new {index="1" }) method="post">
8 <div class="input-group">
9 <input type="text" id="searchkey" name="searchkey" class="form-control" placeholder="Search..." />
10 <span class="btn input-group-addon" onclick="document.searchform.submit();">
11 <span class="glyphicon glyphicon-search"></span>
12 </span>
13 </div>
14 </form>
15 <table class="table table-hover table-bordered table-condensed">
16 <thead>
17 <tr>
18 <th>Name</th>
19 <th>Age</th>
20 </tr>
21 </thead>
22 <tbody>
23 @foreach (var item in Model)
24 {
25 <tr>
26 <td>@item.Name</td>
27 <td>@item.Age</td>
28 </tr>
29 }
30
31 </tbody>
32 </table>
33 @Html.Partial("MvcPagerView", ViewData["pagemodel"])

Ok 搞定。效果如下:

分页的样式我还是比较喜欢的,当然可以自己扩展。

 

基于Bootstrap的Asp.net Mvc 分页的更多相关文章

  1. 基于Bootstrap的Asp.net Mvc 分页的实现

    最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这边是模拟了一些 ...

  2. 基于Bootstrap的Asp.net Mvc 分页的实现(转)

    最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这 边是模拟了一 ...

  3. ASP.NET MVC分页组件MvcPager 2.0版发布暨网站全新改版

    MvcPager分页控件是在ASP.NET MVC Web应用程序中实现分页功能的一系列扩展方法,该分页控件的最初的实现方法借鉴了网上流行的部分源代码, 尤其是ScottGu的PagedList< ...

  4. ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页

    我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了 ...

  5. 基于C#和Asp.NET MVC开发GPS部标视频监控平台

    基于C#和Asp.NET MVC开发GPS部标监控平台 目前整理了基于.NET技术的部标平台开发文章,可以参考: 1.部标Jt808协议模拟终端的设计和开发 2.C#版的808GPS服务器开发-> ...

  6. ASP.NET MVC分页实现

    ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FO ...

  7. 基于C#和Asp.NET MVC开发GPS部标监控平台

    基于交通部796标准开发部标监控平台,选择开发语言和技术也是团队要思考的因素,其实这由团队自己擅长的技术来决定,如果擅长C#和Asp.NET, 当然开发效率就高很多.当然了技术选型一定要选用当前主流的 ...

  8. Bootstrap 与 ASP.NET MVC 4 不使用 NuGet Package 笔记

    转自 http://www.mytecbits.com/microsoft/dot-net/bootstrap-with-asp-net-mvc-4-step-by-step 单位最近做了一个Boot ...

  9. Asp.net MVC分页实例

    分页是网页基本功能,这里主要讨论在Asp.net MVC环境下分页的前端实现,不涉及后台分页.实现效果如下图显示: Step 1.建立分页信息类 public class PagingInfo { p ...

随机推荐

  1. 教你使用vim表白

    99669999996669999996699666699666999966699666699 99699999999699999999699666699669966996699666699 9966 ...

  2. SQL Server 优化存储过程的七种方法

    原文:SQL Server 优化存储过程的七种方法 优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会 ...

  3. Vertica对于所计算的时间SQL声明大全

    词:强.大.所有,强烈推荐 SQL语句 查询结果 select (timestamp '2005-01-17 10:00' - timestamp '2005-01-01'); 16 10:10 se ...

  4. Repository、IUnitOfWork 和 IDbContext 的实践

    Repository.IUnitOfWork 和 IDbContext 的实践 好久没写 DDD 领域驱动设计相关的文章了,嘎嘎!!! 这几天在开发一个新的项目,虽然不是基于领域驱动设计的,但我想把 ...

  5. Android-往来:添加到联系人

    //添加到联系人.使用事务 public void testAddContact() { String name[]={"周杰伦","谢霆锋","言承 ...

  6. java jdk缓存-128~127的Long与Integer

    先推断下以下代码的输出结果 Qa:----------------------------------------------           Long a = Long.valueOf(127) ...

  7. 设计模式 - 迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释

    迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考迭代器模式(ite ...

  8. Android发展简报

    Android这个词的本义是指“机器人”.同时它是Google至2007年11月5根据公布Linux台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成.号称是首个为移动终端打 ...

  9. Sql Server脚本使用TFS版本控制

    原文:Sql Server脚本使用TFS版本控制 1.安装TFS插件 Microsoft Visual Studio Team Foundation Server 2010 MSSCCI Provid ...

  10. ubuntu分屏终端

    一:更新source 列表: 将/etc/apt/sources.list的镜像源改为网易源:http://mirrors.163.com/ubuntu/dists/ deb http://mirro ...