基于Bootstrap的Asp.net Mvc 分页的实现
最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下。首先新建一个Mvc 项目,既然是分页就需要一些数据,我这边是模拟了一些假的数据,实际的项目中都是在数据库中去取得的,很简单的数据:
public class User
{
public string Name { get; set; } public int Age { get; set; }
}
取数据我这边是加了120个数据:
public List<User> GetData()
{
List<User> list = new List<User>();
string[] array = new string[] { "Tom", "Joy", "James", "Kobe", "Jodan", "LiLei", "Hanmeimei", "xiaoming", "Danneil", "Forest", "Newbee", "Azure" };
for (int i = ; i < ; i++)
{
User user = new User();
user.Age = i;
user.Name = array[i / ];
list.Add(user);
} return list;
}
然后新建一个 PageModel类
/// <summary>
/// 有些属性我写成了虚的, 这样可以根据不同的需要去重写便于扩展
/// </summary>
public class BasePageModel
{
public string SearchKeyWord { get; set; } /// <summary>
///点击分页是指向 Action 的名字 根据具体需要而定
/// </summary>
public virtual string ActionName
{
get
{
return "Index";
}
} public int TotalCount { get; set; } public int CurrentIndex { get; set; } public int TotalPages
{
get
{
return (int)Math.Ceiling((double)TotalCount / (double)PageSize);
}
} /// <summary>
/// 根据需要具体而定PageSize
/// </summary>
public virtual int PageSize
{
get { return ; }
} /// <summary>
///根据需要具体而定 分页显示最大的页数
/// </summary>
public virtual int DisplayMaxPages
{
get
{
return ;
}
} public bool IsHasPrePage
{
get
{
return CurrentIndex != ;
}
} public bool IsHasNextPage
{
get
{
return CurrentIndex != TotalPages;
}
}
}
再新建一个分布式图 建在Shared 文件夹里,代码如下:
@using MvcTest.Models
@model MvcTest.Models.BasePageModel @{if (Model != null && Model.TotalPages != )
{
<ul class="pagination">
@{ @Url.CreatPageLiTag(Model, Model.CurrentIndex - , false, Model.IsHasPrePage, "«") if (Model.TotalPages <= Model.DisplayMaxPages)
{
for (int i = ; i < Model.TotalPages; i++)
{
@Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);
}
}
else
{
if (Model.CurrentIndex - < )
{
for (int i = ; i <= Model.DisplayMaxPages - ; i++)
{
@Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);
} @Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");
}
else
{
@Url.CreatPageLiTag(Model, ); if (Model.CurrentIndex + (Model.DisplayMaxPages - ) / >= Model.TotalPages)
{
int page = Model.CurrentIndex - (Model.DisplayMaxPages - Model.TotalPages + Model.CurrentIndex - ); if (page > )
{
@Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");
} for (int i = page + ; i < Model.TotalPages; i++)
{
@Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);
}
}
else
{
int page = Model.CurrentIndex - (Model.DisplayMaxPages - ) / ; if (page > )
{
@Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");
} for (int i = page; i < Model.CurrentIndex + (Model.DisplayMaxPages - ) / ; i++)
{
@Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);
}
@Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");
} }
} @Url.CreatPageLiTag(Model, Model.TotalPages, Model.TotalPages == Model.CurrentIndex)
@Url.CreatPageLiTag(Model, Model.CurrentIndex + , false, Model.IsHasNextPage, "»") }
</ul> }}
以上就是分页的核心代码,包括了一些判断逻辑,其中的 @Url.CreatPageLiTag 我是写了一个扩展
public static class HtmlHelperExtensions
{
public static MvcHtmlString CreatPageLiTag(this UrlHelper urlHelper,
BasePageModel pageModel,
int index,
bool isCurrentIndex = false,
bool isDisable = true,
string content = "")
{ string url = urlHelper.Action(pageModel.ActionName, new { searchkey = pageModel.SearchKeyWord, index = index });
string activeClass = !isCurrentIndex ? string.Empty : "class='active'";
string disableClass = isDisable ? string.Empty : "class='disabled'";
url = isDisable ? "href='" + url + "'" : string.Empty;
string contentString = string.IsNullOrEmpty(content) ? index.ToString() : content; return new MvcHtmlString("<li " + activeClass + disableClass + "><a " + url + ">" + contentString + "</a></li>");
}
}
在这里面里面 是生成<a/>标签的,样式可以自己定。无非就是一些css 的定义。
然后就在action 的方法里取数据
public ActionResult Index(string searchkey, string index)
{
if (string.IsNullOrEmpty(index))
index = "";
if (string.IsNullOrEmpty(searchkey))
searchkey = string.Empty; List<User> totalList = GetData().Where(p=>p.Name.ToLower().Contains(searchkey.ToLower())).ToList();
BasePageModel page = new BasePageModel() { SearchKeyWord = searchkey, CurrentIndex = Int32.Parse(index), TotalCount = totalList.Count }; List<User> pageList = totalList.Skip((page.CurrentIndex - ) * page.PageSize).Take(page.PageSize).ToList();
ViewData["pagemodel"] = page;
return View(pageList);
}
前台代码:
@model List<MvcTest.Controllers.User>
@{
ViewBag.Title = "Index";
} <h2>Data List</h2>
<form class="navbar-form navbar-right" name="searchform" action="@Url.Action("Index", new {index="1" }) method="post">
<div class="input-group">
<input type="text" id="searchkey" name="searchkey" class="form-control" placeholder="Search..." />
<span class="btn input-group-addon" onclick="document.searchform.submit();">
<span class="glyphicon glyphicon-search"></span>
</span>
</div>
</form>
<table class="table table-hover table-bordered table-condensed">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.Age</td>
</tr>
} </tbody>
</table>
@Html.Partial("MvcPagerView", ViewData["pagemodel"])
Ok 搞定。效果如下:
分页的样式我还是比较喜欢的,当然可以自己扩展。
基于Bootstrap的Asp.net Mvc 分页的实现的更多相关文章
- 基于Bootstrap的Asp.net Mvc 分页
基于Bootstrap的Asp.net Mvc 分页的实现 最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一 ...
- 基于Bootstrap的Asp.net Mvc 分页的实现(转)
最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这 边是模拟了一 ...
- ASP.NET MVC分页组件MvcPager 2.0版发布暨网站全新改版
MvcPager分页控件是在ASP.NET MVC Web应用程序中实现分页功能的一系列扩展方法,该分页控件的最初的实现方法借鉴了网上流行的部分源代码, 尤其是ScottGu的PagedList< ...
- ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了 ...
- 基于C#和Asp.NET MVC开发GPS部标视频监控平台
基于C#和Asp.NET MVC开发GPS部标监控平台 目前整理了基于.NET技术的部标平台开发文章,可以参考: 1.部标Jt808协议模拟终端的设计和开发 2.C#版的808GPS服务器开发-> ...
- ASP.NET MVC分页实现
ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FO ...
- 基于C#和Asp.NET MVC开发GPS部标监控平台
基于交通部796标准开发部标监控平台,选择开发语言和技术也是团队要思考的因素,其实这由团队自己擅长的技术来决定,如果擅长C#和Asp.NET, 当然开发效率就高很多.当然了技术选型一定要选用当前主流的 ...
- Bootstrap 与 ASP.NET MVC 4 不使用 NuGet Package 笔记
转自 http://www.mytecbits.com/microsoft/dot-net/bootstrap-with-asp-net-mvc-4-step-by-step 单位最近做了一个Boot ...
- Asp.net MVC分页实例
分页是网页基本功能,这里主要讨论在Asp.net MVC环境下分页的前端实现,不涉及后台分页.实现效果如下图显示: Step 1.建立分页信息类 public class PagingInfo { p ...
随机推荐
- SQL2005函数大全
表达式:是常量.变量.列或函数等与运算符的任意组合.以下参数中表达式类型是指表达式经运算后返回的值的类型 字符串函数 函数名称 参数 示例 说明 ascii (字符串表达式) select ascii ...
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session异常解决办法
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...
- bzoj千题计划160:bzoj2599: [IOI2011]Race
http://www.lydsy.com/JudgeOnline/problem.php?id=2599 点分治 mi[i] 记录边权和为i时的最少边数 先更新答案,再更新mi数组,换根时清空mi # ...
- Ubuntu下快速部署安装 Nginx + PHP + MySQL 笔记
先更新软件库 sudo apt-get update 安装 MySQL sudo apt-get install mysql-server 安装 Nginx sudo apt-get inst ...
- [转载]memset()的效率
http://blog.csdn.net/hackbuteer1/article/details/7343189 void *memset(void *s, int ch, size_t n); 作用 ...
- c++刷题(27/100)反转单项链表,链表的倒数第k个
题目1:调整数组顺序使奇数位于偶数前面 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位 ...
- 【译】SSH隧道:本地和远程端口转发
本文是:SSH Tunnel - Local and Remote Port Forwarding Explained With Examples 的译文 有两种方法可以创建SSH隧道,本地和远程端口 ...
- 13、Math类简介
Math类概述 在java.lang包下,有个Math类,这个类包含用于执行基本数学运算的方法,如四舍五入,开方等等. package com.sutaoyu.usually_class; publi ...
- c# 生成随机N位数字串(每位可以重复)
/// <summary> /// 生成随机数字窜 /// </summary> /// <param name="Digit">位数</ ...
- 【译】第八篇 Integration Services:高级工作流管理
本篇文章是Integration Services系列的第八篇,详细内容请参考原文. 简介在前面两篇文章,我们创建了一个新的SSIS包,学习了SSIS中的脚本任务和优先约束,并检查包的MaxConcu ...