Boostrap分页控件比较美观,

控制器代码:使用仓储模式实现。如果是直接使用DbContext上下文使用更简单。

public ActionResult Index(int? page,string categoryName,string SearchTitle)
{
var categoryNameList = new List<string>(); //可考虑使用hashSet<>
var categoryQuery = from c in _articleService.FindAll()
select c.Category.CategoryName;
categoryNameList.AddRange(categoryQuery.Distinct());
ViewBag.categoryNameList = new SelectList(categoryNameList, categoryName);
ViewBag.categoryName = categoryName;

ViewBag.SearchTitle = SearchTitle;

int pageNumber = page ?? 1;  //如果page  参数没有赋值,默认为1。??空值合并运算符,意思是如果page有值,就为page的值,如果page 为空,值就为1.

ViewBag.page = pageNumber;

int recordPerPage =3;    //设置每一页记录数。

ViewBag.recordPerPage = recordPerPage;
int totalRecord = 0;   //定义一个输出参数。其实这里给输出参数赋值为0,是没多大意义的。只是为了能够访问到这个变量名而已。输出参数就像引用参数一样,跟形参占用相同的内存空间,形参值改变了,输出参数值也变了。 输出参数是为了解决一个C#中一个函数只能返回一个值的问题,使用它能返回多个值。
IEnumerable<Article> articles;
if (string.IsNullOrEmpty(categoryName))
{
if(string.IsNullOrEmpty(SearchTitle))
articles = _articleService.FindPageList<DateTime>(pageNumber, recordPerPage, out totalRecord, a =>true, OrderType.Desc, a => a.PostTime);
else
articles = _articleService.FindPageList<DateTime>(pageNumber, recordPerPage, out totalRecord, a => a.Title.Contains(SearchTitle), OrderType.Desc, a => a.PostTime);
}
else
{
if (string.IsNullOrEmpty(SearchTitle))
articles = _articleService.FindPageList<DateTime>(pageNumber,recordPerPage,out totalRecord,a=>a.Category.CategoryName == categoryName,OrderType.Desc,a =>a.PostTime);
else
articles = _articleService.FindPageList<DateTime>(pageNumber, recordPerPage, out totalRecord, a => a.Category.CategoryName == categoryName && a.Title.Contains(SearchTitle), OrderType.Desc, a => a.PostTime);

}

ViewBag.totalRecord = totalRecord;
ViewBag.totalPage =(int)Math.Ceiling((double)totalRecord/(double)recordPerPage);      //Math.Celling()向上取整函数。 同样,Math.Floor()向下取整函数。

return View(articles.ToList());
}

视图:

@model IEnumerable<MajorConstruction.Models.Article>

@{
ViewBag.Title = "文章列表";
}

<h2>@ViewBag.Title</h2>

@using (Html.BeginForm("Index", "Article", FormMethod.Get, new { @class = "form-inline", role = "form" }))   //内联表单,显示在一行上。
{
<label for="categoryName" class="control-label">栏目名称:</label>
<div class="form-group">
@Html.DropDownList("categoryName", ViewBag.categoryNameList as SelectList, "全部栏目", new { @class="form-control"})
</div>

<label for="searchTextbox" class="control-label"> 通过标题查找:</label>
<div class="form-group">
@Html.TextBox("SearchTitle", ViewBag.SearchTitle as string, new { @class = "form-control" })
</div>

<input type="submit" value="查找" class="btn btn-primary" />

}
<table class="table table-striped table-hover">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Category.CategoryName)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.AuthorName)
</th>
<th>
@Html.DisplayNameFor(model => model.PostTime)
</th>
<th>
@Html.DisplayNameFor(model => model.PriorOrder)
</th>
<th>
@Html.DisplayNameFor(model => model.ClickCount)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Category.CategoryName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.AuthorName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PostTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.PriorOrder)
</td>
<td>
@Html.DisplayFor(modelItem => item.ClickCount)
</td>

<td>
@Html.ActionLink("编辑", "Edit", new { id = item.ArticleID }) |
@Html.ActionLink("预览", "Details", new { id = item.ArticleID }) |
@Html.ActionLink("删除", "Delete", new { id = item.ArticleID })
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td class="text-info" colspan="5">
每页 @ViewBag.recordPerPage 条记录,共有 @ViewBag.totalRecord 条记录。 第 @(ViewBag.totalRecord == 0 ? 0 : ViewBag.page) 页 ,共 @ViewBag.totalPage 页  //如果查询到的记录数为0,就显示为第0页。这里有一个条件表达式的目的是为了避免 如  第1页,共0页。的情况。
</td>
</tr>
</tfoot>
</table>

@if (ViewBag.totalRecord != 0)       //是为了避免出现没有记录,还是显示下一页的符号链接。
{
<ul class="pagination">
@if (ViewBag.page != 1)  //如果当前页面不是第1页,就显示 <<上一页的符号链接。当前页面是第1页,就不显示<<了。
{
<li><a href="@Url.Action("Index", new { categoryName= ViewBag.categoryName,SearchTitle=ViewBag.SearchTitle, page =(int)(ViewBag.page) -1})">&laquo;</a></li>   //为了保证分页与筛选功能的一致性,所以在链接中增加了路由参数。并将当前值通过ViewBag回传给各个输入表单字段。
}

@for (int page = 1; page <= (int)@ViewBag.totalPage; page++)
{
string activeCss = page == (int)ViewBag.page ? "active" : null;
<li class="@activeCss"><a href="@Url.Action("Index", new { categoryName = ViewBag.categoryName,SearchTitle=ViewBag.SearchTitle, page = page })">@page</a></li>

}
@if (ViewBag.page != ViewBag.totalPage)  //如果当前页面不是最后一页了,就显示 >>下一页的符号链接。当前页面是最后一页,就不显示>>了。
{
<li><a href="@Url.Action("Index", new { categoryName = ViewBag.categoryName,SearchTitle=ViewBag.SearchTitle, page = (int)(ViewBag.page) + 1 })">&raquo;</a></li>
}

</ul>
}

asp.net MVC 使用Bootstrap 分页的更多相关文章

  1. ASP.NET MVC使用Bootstrap系列(3)——使用Bootstrap 组件

    阅读目录 Bootstrap 导航条 列表组 徽章 媒体对象 页头 路径导航 分页 输入框组 按钮式下拉菜单 警告框 进度条 小结 Bootstrap为我们提供了十几种的可复用组件,包括字体图标.下拉 ...

  2. [ASP.NET MVC] 使用Bootstrap套件

    [ASP.NET MVC] 使用Bootstrap套件 前言 在开发Web项目的时候,除了一些天赋异禀的开发人员之外,大多数的开发人员应该都跟我一样,对于如何建构出「美观」的用户接口而感到困扰.这时除 ...

  3. ASP.NET MVC利用PagedList分页(二)PagedList+Ajax+JsRender

    (原文) 昨天在ASP.NET MVC利用PagedList分页(一)的 最后一节提到,一个好的用户体验绝对不可能是点击下一页后刷新页面,所以今天来说说利用Ajax+PagedList实现无刷新(个人 ...

  4. asp.net mvc easyui datagrid分页

    提到 asp.net mvc 中的分页,很多是在用aspnetpager,和easyui datagrid结合的分页却不多,本文介绍的是利用easyui 中默认的分页控件,实现asp.net mvc分 ...

  5. 基于ASP.NET MVC和Bootstrap搭建响应式个人博客站(一)

    1.0 为什么要做这个博客站? www.zynblog.com   在工作学习中,经常要搜索查找各种各样的资料,每次找到相关资料后都会顺手添加到浏览器书签中,时间一长,书签也就满了.而且下次再点击这个 ...

  6. ASP.NET MVC利用PagedList分页(一)

    前几天看见博客园上有人写ASP.NET MVC的分页思想,这让我不禁想起了PagedList.PagedList是NuGet上提供的一个分页的类库,能对任何IEnumerable<T>进行 ...

  7. asp.net mvc多条件+分页查询解决方案

    开发环境vs2010 css:bootstrap js:jquery bootstrap paginator 原先只是想做个mvc的分页,但是一般的数据展现都需要检索条件,而且是多个条件,所以就变成了 ...

  8. ASP.NET MVC使用Bootstrap系列(5)——创建ASP.NET MVC Bootstrap Helpers

    阅读目录 序言 内置的HTML Helpers 创建自定义的Helpers 使用静态方法创建Helpers 使用扩展方法创建Helpers 创建Fluent Helpers 创建自动闭合的Helper ...

  9. ASP.NET MVC使用Bootstrap系列(4)——使用JavaScript插件

    阅读目录 序言 Data属性 VS 编程API 下拉菜单(dropdown.js) 模态框(modal.js) 标签页(tab.js) 工具提示(tooltip.js) 弹出框(popover.js) ...

随机推荐

  1. [转]eclipse的android智能提示设置

    以往 我们往往在输入 "." 然后 alt+/ 来进行智能提示,下面这个方法,可以帮你大幅度的提高智能打开 Eclipse -> Window -> Perferenc ...

  2. windows7 下安装使用Redis

    Redis 安装使用 本地环境:Windows7 64位web环境:wamp集成环境,php版本:PHP Version 7.1.17 学习参考网站: RUNOOB.COM官网  http://www ...

  3. codeforces 1041 c 乱搞

    #include <bits/stdc++.h> using namespace std; struct po { int val; int id; }; po a[]; vector&l ...

  4. 从零开始写STL—set/map

    这一部分只要把搜索树中暴露的接口封装一下,做一些改动. set源码剖析 template<typename T> class set { public: typedef T key_typ ...

  5. LinkedList总结

    1,LinkedList也是继承了List的接口 所以在LinkedList中存储的也是有序的,不唯一的数据 它采用的是链表式储存,所以比较适合用来执行插入,删除等功能 2,LinkedList特有的 ...

  6. 洛谷 P1731 [NOI1999]生日蛋糕

    P1731 [NOI1999]生日蛋糕 题目背景 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层 生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1<=i<=M ...

  7. xml解析工具mashaller javaee自带解析类

    1.怎样去掉Marshaller的格式化? : JAXBContext context = JAXBContext.newInstance(Entity.class); Marshaller mars ...

  8. [转]JAVA对象容器

    要用Java实现记事本的功能.首先列出记事本所需功能: 可以添加记录(字符串): 可以获得记录条数: 可以删除其中某一条记录: 可以获得指定第几条的记录: 可以列出所有的记录. 如果这个记事本是某个大 ...

  9. 深入源代码解析Android中的Handler,Message,MessageQueue,Looper

    本文主要是对Handler和消息循环的实现原理进行源代码分析.假设不熟悉Handler能够參见博文< Android中Handler的使用>,里面对Android为何以引入Handler机 ...

  10. 竞赛中经常使用的C++写法

    首先是构造函数,重载 #include <iostream> #include <cstdio> #include <cstring> #include <s ...