asp.net MVC 使用Bootstrap 分页
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})">«</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 })">»</a></li>
}
</ul>
}
asp.net MVC 使用Bootstrap 分页的更多相关文章
- ASP.NET MVC使用Bootstrap系列(3)——使用Bootstrap 组件
阅读目录 Bootstrap 导航条 列表组 徽章 媒体对象 页头 路径导航 分页 输入框组 按钮式下拉菜单 警告框 进度条 小结 Bootstrap为我们提供了十几种的可复用组件,包括字体图标.下拉 ...
- [ASP.NET MVC] 使用Bootstrap套件
[ASP.NET MVC] 使用Bootstrap套件 前言 在开发Web项目的时候,除了一些天赋异禀的开发人员之外,大多数的开发人员应该都跟我一样,对于如何建构出「美观」的用户接口而感到困扰.这时除 ...
- ASP.NET MVC利用PagedList分页(二)PagedList+Ajax+JsRender
(原文) 昨天在ASP.NET MVC利用PagedList分页(一)的 最后一节提到,一个好的用户体验绝对不可能是点击下一页后刷新页面,所以今天来说说利用Ajax+PagedList实现无刷新(个人 ...
- asp.net mvc easyui datagrid分页
提到 asp.net mvc 中的分页,很多是在用aspnetpager,和easyui datagrid结合的分页却不多,本文介绍的是利用easyui 中默认的分页控件,实现asp.net mvc分 ...
- 基于ASP.NET MVC和Bootstrap搭建响应式个人博客站(一)
1.0 为什么要做这个博客站? www.zynblog.com 在工作学习中,经常要搜索查找各种各样的资料,每次找到相关资料后都会顺手添加到浏览器书签中,时间一长,书签也就满了.而且下次再点击这个 ...
- ASP.NET MVC利用PagedList分页(一)
前几天看见博客园上有人写ASP.NET MVC的分页思想,这让我不禁想起了PagedList.PagedList是NuGet上提供的一个分页的类库,能对任何IEnumerable<T>进行 ...
- asp.net mvc多条件+分页查询解决方案
开发环境vs2010 css:bootstrap js:jquery bootstrap paginator 原先只是想做个mvc的分页,但是一般的数据展现都需要检索条件,而且是多个条件,所以就变成了 ...
- ASP.NET MVC使用Bootstrap系列(5)——创建ASP.NET MVC Bootstrap Helpers
阅读目录 序言 内置的HTML Helpers 创建自定义的Helpers 使用静态方法创建Helpers 使用扩展方法创建Helpers 创建Fluent Helpers 创建自动闭合的Helper ...
- ASP.NET MVC使用Bootstrap系列(4)——使用JavaScript插件
阅读目录 序言 Data属性 VS 编程API 下拉菜单(dropdown.js) 模态框(modal.js) 标签页(tab.js) 工具提示(tooltip.js) 弹出框(popover.js) ...
随机推荐
- Cmder使用总结
windows cmd 使用不方便之处: 1.窗口size不能便捷缩放 2.复制文本,不能直接用鼠标拷贝,还需要多一道菜单操作:而且,还只能块状拷贝,而不是按行字符,极其不便 3.不支持多Tab页,多 ...
- [Usaco2006 Nov] Fence Repair 切割木板
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1356 Solved: 714[Submit][Status][Discuss] Description ...
- 【转载】javascript中的函数对象
原文地址:http://www.cnblogs.com/phpzxh/archive/2009/09/16/1568137.html[侵删] 在javascript中函数的申明方式有四种 下面代码中一 ...
- IE下IFrame引用跨域站点页面时,Session失效问题解决
问题场景:在一个应用(集团门户)的某个page中, 通过IFrame的方式嵌入另一个应用(集团实时监管系统)的某个页面. 当两个应用的domain 不一样时, 在被嵌入的页面中Session失效.(s ...
- python学习之-- shelve模块
shelve模块定义:是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle(因为封装了pickle)可支持的python数据格式.功能:可以实现pickle的多次dump和多次 ...
- P1195 口袋的天空 洛谷
https://www.luogu.org/problem/show?pid=1195 题目背景 小杉坐在教室里,透过口袋一样的窗户看口袋一样的天空. 有很多云飘在那里,看起来很漂亮,小杉想摘下那样美 ...
- Java面试题总结(一)---Java基础
Java面试题总结(一)---Java基础 1.面向对象的特征有哪些? 答:面向对象的特征主要有以下几个: 1)抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方 ...
- 【SDCC讲师专访】PingCAP联合创始人兼CEO刘奇:好的产品应开源,不闭门造车-CSDN.NET
[SDCC讲师专访]PingCAP联合创始人兼CEO刘奇:好的产品应开源,不闭门造车-CSDN.NET 小米的Themis
- [教程]Delphi 中三种回调函数形式解析
Delphi 支持三种形式的回调函数 全局函数这种方式几乎是所有的语言都支持的,类的静态函数也可以归为此类,它保存的只是一个函数的代码起始地址指针( Pointer ).在 Delphi 中声明一般为 ...
- Ansible 详细用法说明(一)
一.概述 运维工具按需不需要有代理程序来划分的话分两类: agent(需要有代理工具):基于专用的agent程序完成管理功能,puppet, func, zabbix agentless(无须代理工具 ...