基于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 = 0; i < 120; i++)

{

User user = new User();

user.Age = i;

user.Name = array[i / 10];

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 10; }

}

/// <summary>

///根据需要具体而定 分页显示最大的页数

/// </summary>

public virtual int DisplayMaxPages

{

get

{

return 10;

}

}

public bool IsHasPrePage

{

get

{

return CurrentIndex != 1;

}

}

public bool IsHasNextPage

{

get

{

return CurrentIndex != TotalPages;

}

}

}

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

@using MvcTest.Models

@model MvcTest.Models.BasePageModel

@{if (Model != null && Model.TotalPages != 0)

{

<ul class="pagination">

@{

@Url.CreatPageLiTag(Model, Model.CurrentIndex - 1, false, Model.IsHasPrePage, "&laquo;")

if (Model.TotalPages <= Model.DisplayMaxPages)

{

for (int i = 1; i < Model.TotalPages; i++)

{

@Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);

}

}

else

{

if (Model.CurrentIndex - 1 < 5)

{

for (int i = 1; i <= Model.DisplayMaxPages - 1; i++)

{

@Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);

}

@Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");

}

else

{

@Url.CreatPageLiTag(Model, 1);

if (Model.CurrentIndex + (Model.DisplayMaxPages - 2) / 2 >= Model.TotalPages)

{

int page = Model.CurrentIndex - (Model.DisplayMaxPages - Model.TotalPages + Model.CurrentIndex - 1);

if (page > 1)

{

@Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");

}

for (int i = page + 1; i < Model.TotalPages; i++)

{

@Url.CreatPageLiTag(Model, i, i == Model.CurrentIndex);

}

}

else

{

int page = Model.CurrentIndex - (Model.DisplayMaxPages - 2) / 2;

if (page > 2)

{

@Url.CreatPageLiTag(Model, Model.CurrentIndex, false, false, "...");

}

for (int i = page; i < Model.CurrentIndex + (Model.DisplayMaxPages - 2) / 2; 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 + 1, false, Model.IsHasNextPage, "&raquo;")

}

</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 = "1";

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 - 1) * 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 搞定。效果如下:

时间: 2024-08-25 08:25:25

基于Bootstrap的Asp.net Mvc 分页的实现(转)的相关文章

基于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 publ

Asp.Net MVC 分页、检索、排序整体实现

原文:Asp.Net MVC 分页.检索.排序整体实现 很多时候需要这样的功能,对表格进行分页.排序和检索.这个有很多实现的方式,有现成的表格控件.用前端的mvvm,用户控件.但很多时候看着很漂亮的东西你想进一步控制的时候却不那么如意.这里自己实现一次,功能不是高大全,但求一个清楚明白,也欢迎园友拍砖.前端是bootstrap3+jPaginate,后台基于membership.没什么难点. 先上效果图. 分页其实就是处理好 每页项目数.总项目数.总页数.当前页.为了方便复用,就先从仓库开始说起

ASP.NET MVC 分页MvcPager

ASP.NET MVC 分页MvcPager 开源框架  admin  8个月前 (08-20)  1484浏览 他连续12年获得微软MPV称号,几年前,他写的ASP.NET分页控件,被很多.NET开发人员使用,现在他又写了名为MvcPager的分页扩展,并免费开源,支持MVC通用分页,与EF完美结合,支持AJAX分页,简单灵活,提供多个演示案例,是迄今为止最好的MVC分页方式,推荐各位ASP.NET MVC开发者使用. 分页是每个项目必须面对的技术点,不好的分页不但体验不好,而且会影响系统的整

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

我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了更为灵活的配置属性及生成规则,解决了上述问题,代码如下: 一.PageInfo类 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace ROIS.Models {

ASP.NET MVC分页实现

ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FORM中,当然以后我会考虑实现基于URL分页的! 一.PageInfo类 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace ROIS.Mo

Bootstrap in ASP.NET MVC 5

一,新建ASP.NET MVC 5 项目 Bootstrap 文件分布 引入到页面 1.定义.注意:不要包含有.min.的文件名称,会被忽略,因为在发布的时候编译器会加载min版的文件 2.在母版页中引用上面的定义 最后一步,程序启动时加载定义的绑定 启用压缩js,css 运行后看页面源码: 压缩前时这样的:

Asp.net MVC分页实例

分页是网页基本功能,这里主要讨论在Asp.net MVC环境下分页的前端实现,不涉及后台分页.实现效果如下图显示: Step 1.建立分页信息类 1 public class PagingInfo 2 { 3 public int TotalItmes { set; get; } 4 public int ItemsPerPage { set; get; } 5 public int CurrentPage { set; get; } 6 public int TotalPages 7 { 8

【随笔系列】Asp.Net Mvc分页控件PagedList的使用方法及配置

企业在做Asp.Net Mvc开发过程中,很多时候都是一些CRUD,最基本的就是一个列表页面,然后附带一些功能按钮.如果有数据列表,大多数就会涉及到对数据进行分页,这次就介绍一下Mvc PagedList控件分页的使用方法.Github PagedList链接 . 下面我通过新建Mvc项目来展示PagedList的使用方法. 一.新建BookLibrary解决方案 确定后,选择MVC 然后点击确定. 二.添加PagedList与PagedList.Mvc的程序包. 选择BookLibrary项目

ASP.NET MVC分页 Ajax+JsRender

前段时间整mvc的分页,倒是很顺利,参考了以下几篇博客,启发很大. http://www.cnblogs.com/tangmingjun/archive/2012/05/30/2526301.html http://www.cnblogs.com/tangmingjun/archive/2012/05/31/2528732.html 顺便扩展了需求,在分页的基础上,继续做关键字查询. 用PagedList生成的页码链接虽然样式很漂亮,但是要做到无刷新的分页,PagedList自动生成的代码是不够