ASP.NET MVC 扩展HtmlHelper类方法
1、扩展HtmlHelper类方法ShowPageNavigate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
public static HtmlString ShowPageNavigate( this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount) { var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath; pageSize = pageSize == 0 ? 3 : pageSize; var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数 var output = new StringBuilder(); if (totalPages > 1) { output.AppendFormat( "<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a> " , redirectTo, pageSize); if (currentPage > 1) { //处理上一页的连接 output.AppendFormat( "<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a> " , redirectTo, currentPage - 1, pageSize); } output.Append( " " ); int currint = 5; for ( int i = 0; i <= 10; i++) { //一共最多显示10个页码,前面5个,后面5个 if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages) { if (currint == i) { //当前页处理 output.AppendFormat( "<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> " , redirectTo, currentPage, pageSize, currentPage); } else { //一般页处理 output.AppendFormat( "<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> " , redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint); } } output.Append( " " ); } if (currentPage < totalPages) { //处理下一页的链接 output.AppendFormat( "<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a> " , redirectTo, currentPage + 1, pageSize); } output.Append( " " ); if (currentPage != totalPages) { output.AppendFormat( "<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a> " , redirectTo, totalPages, pageSize); } output.Append( " " ); } output.AppendFormat( "<label>第{0}页 / 共{1}页</label>" , currentPage, totalPages); //这个统计加不加都行 return new HtmlString(output.ToString()); } |
2、添加公共类PagerInfo,PageQuery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class PagerInfo { public int RecordCount { get ; set ; } public int CurrentPageIndex { get ; set ; } public int PageSize { get ; set ; } } public class PagerQuery<TPager, TEntityList> { public PagerQuery(TPager pager, TEntityList entityList) { this .Pager = pager; this .EntityList = entityList; } public TPager Pager { get ; set ; } public TEntityList EntityList { get ; set ; } } |
3、然后在Controller里面添加Action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public ActionResult Index( int ? pageSize, int ? pageIndex) { int pageIndex1 = pageIndex ?? 1; int pageSize1 = pageSize ?? 5; int count=0; //从数据库在取得数据,并返回总记录数 var temp = newsSer.LoadPageEntities(c => true , c => c.id, false , pageSize1, pageIndex1, out count); PagerInfo pager = new PagerInfo(); pager.CurrentPageIndex = pageIndex1; pager.PageSize = pageSize1; pager.RecordCount = count; PagerQuery<PagerInfo,IQueryable<news>> query = new PagerQuery<PagerInfo,IQueryable<news>>(pager,temp); return View(query); } |
4、View里的部分代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
< tbody > @foreach (var item in Model.EntityList) { < tr > < td class="checkBox"> < input name="ids[]" type="checkbox" value="" /> </ td > < td > @item.author </ td > < td > @item.title </ td > < td > @item.ctime </ td > < td > @Html.ActionLink("编辑", "Edit", new { id = item.id }) | @Html.ActionLink("删除", "Delete", new { id = item.id }) </ td > </ tr > } @*分页*@ < tr class=""> < td colspan="5" align="center" class="paginator"> < span > @Html.ShowPageNavigate(Model.Pager.CurrentPageIndex, Model.Pager.PageSize, Model.Pager.RecordCount) </ span > </ td > </ tr > </ tbody > |
5、添加一些样式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
.paginator { font: 12px Arial, Helvetica, sans-serif; padding: 10px 20px 10px 0; margin: 0px auto; } .paginator a { border: solid 1px #ccc; color: #0063dc; cursor: pointer; text-decoration: none; } .paginator a:visited { padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none; } .paginator .cpb { border: 1px solid #F50; font-weight: 700; color: #F50;
} .paginator a:hover { border: solid 1px #F50; color: #f60; text-decoration: none; } .paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover { float: left; height: 16px; line-height: 16px; min-width: 10px; _width: 10px; margin-right: 5px; text-align: center; white-space: nowrap; font-size: 12px; font-family: Arial,SimSun; padding: 0 3px; } .paginator label { display:block; float:left; } |
6、运行,得效果图
ASP.NET MVC 扩展HtmlHelper类方法的更多相关文章
- ASP.NET MVC 扩展HtmlHelper类为 js ,css 资源文件添加版本号
写在前面 在项目部署当中会需要更新 css 文件或 js 等资源文件,为了避免由于浏览器缓存的原因无法加载新的 css 或 js ,一般的做法是在资源文件的后面加上一个版本号来解决,这样浏览器就会去服 ...
- ASP.NET MVC扩展库
很多同学都读过这篇文章吧 ASP.NET MVC中你必须知道的13个扩展点,今天给大家介绍一个ASP.NET MVC的扩展库,主要就是针对这些扩展点进行.这个项目的核心是IOC容器,包括Ninject ...
- ASP.NET MVC Razor HtmlHelper扩展和自定义控件
先看示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using S ...
- 17+个ASP.NET MVC扩展点【附源码】
1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig. 在自定义的Http ...
- Asp.Net MVC 扩展 Html.ImageFor 方法详解
背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelper却无法输出一个img,当用脚手架自动生成一些form或表格的时候, ...
- 16个ASP.NET MVC扩展点【附源码】
转载于:http://www.cnblogs.com/wupeiqi/p/3570445.html 1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中! ...
- [转]Asp.Net MVC 扩展联想控件
本文转自:http://www.cnblogs.com/bright-lin/archive/2013/02/06/MVC_SuggestBox.html 在web中,为改善用户体验,我们常会将一些文 ...
- [转]Asp.Net MVC使用HtmlHelper渲染,并传递FormCollection参数的陷阱 【转】
在Asp.Net MVC 1.0编程中,我们经常遇见这样的场景,在新建一个对象时候,通过HtmlHelper的方式在View模型中渲染Html控件,当填写完相关内容后,通过Form把需要新建的内容Po ...
- [转]Asp.Net MVC使用HtmlHelper渲染,并传递FormCollection参数的陷阱
http://www.cnblogs.com/errorif/archive/2012/02/13/2349902.html 在Asp.Net MVC 1.0编程中,我们经常遇见这样的场景,在新建一个 ...
随机推荐
- ES5特性Object.seal
一个对象在默认状态下: 1,extensible:可扩展(可以添加新的属性) 2,configurable:可配置(可以改变原有属性的特性,比如修改属性的enumerable) Object.seal ...
- HTML 学习笔记 CSS样式(文本)
CSS文本属性可以定义文本的外观 通过文本属性 您可以改变文本的颜色 字符间距 文本对齐装饰文本 对文本进行缩进等等. 缩进文本 把web页面上的段落的第一行缩进,这是最常用的文本格式化效果. css ...
- HTML-学习笔记(属性)
HTML属性 HTML 标签可以拥有属性.属性提供了有关HTML元素更多的信息. 属性总是以键值对的形式出现.例如 name = "value"; 属性总是在HTML元素的开始标签 ...
- 链剖&LCT总结
在搞LCT之前,我们不妨再看看喜闻乐见的树链剖分. 树链剖分有一道喜闻乐见的例题:NOI2015 软件包管理器 如果你看懂题目了,你就会明白它是叫你维护一个树,这棵树是不会动的,要兹磁子树求和,子树修 ...
- python设计模式1:导言
<设计模式>一书总结了23个模式,依据各自的目的又被分为创建型模式(creational pattern).结构型模式(structural pattern)和行为型模式(behavior ...
- 安装MySQLdb
MySQLdb模块不太好装,如果是在python2.7环境下,可以使用一种简易安装方式 root@iZ2893wjzgyZ:~# apt-get install python2.7-mysqldb 测 ...
- Linux的chattr与lsattr命令
有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的linux ...
- hadoop:将WordCount打包成独立运行的jar包
hadoop示例中的WordCount程序,很多教程上都是推荐以下二种运行方式: 1.将生成的jar包,复制到hadoop集群中的节点,然后运行 $HADOOP_HOME/bin/hadoop xxx ...
- Qt 学习笔记 TreeWidget 增删改
在窗体上放一个TreeWidget控件和四个PushButton加一个Horizontal Spacer 布局如图 给树添加元素节点的方法和实现 .h文件 QTreeWidgetItem * AddT ...
- 微信公众平台开发(71)OAuth2.0网页授权
微信公众平台开发 OAuth2.0网页授权认证 网页授权获取用户基本信息 作者:方倍工作室 微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使 ...