在建立的mvc3项目中,在Razor(CSHTML)视图引擎下,数据会在表格中自动的生成,但分页没有好的控件实现,这里我们开发了设计了一个分页的模板,适合于没有数据提交和有数据提交的分页的分页。
第一:没有有数据提交的分页功能:
第一步,建立一个 Controller命名为PageIndex的空控制器,自定义一个方法如下:
public ActionResult PageIndex(string action, string controller, int currentPage, int pageCount)
{
//int count = db.Product.Count();
ViewBag.PageCount = pageCount;//从操作中获取总数据页数将传入分页视图页面
ViewBag.CurrentPage = currentPage;//从操作中获取当前页数将传入分页视图页面
ViewBag.action = action;
ViewBag.controller = controller;
return PartialView();
}
传入四个参数,分别为:操作,控制器,当前页数,数据总页数,操作是指你要分页的试图的操作一般为Index,控制器为该试图对应的控制器, 这四个都为视图传递数据的。
第二步:在PageIndex控制器下的Index上添加视图选择分布视图,代码如下:
@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
{
<span>您好,当前没有数据显示!</span>
}
else
{
if (ViewBag.CurrentPage <= 10)
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">
首页</a>|</span>
}
else
{
<a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">
首页</a>
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 10 }, null)">
...</a> </span>
}
for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
{
if (i <= 0)
{
continue;
}
if (i > ViewBag.PageCount)
{
break;
}
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = i }, null)">
第 @i 页</a>|</span>
}
if (ViewBag.CurrentPage >1)
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 1 }, null)">
上一页</a>|</span>
}
if (ViewBag.PageCount>ViewBag.CurrentPage )
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 1 }, null)">
下一页</a></span>
}
if (ViewBag.CurrentPage == ViewBag.PageCount || ViewBag.CurrentPage >= ViewBag.PageCount - 10)
{
<a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">
尾 页</a>
}
else
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 10 }, null)">
...</a></span>
<a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">
尾 页</a>
}
<span style="padding-left: 20px">当前页数: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 页
</span>
} 在操纵中(Index)的部分代码: public ViewResult Index(int? pageIndex) { int pageInd = pageIndex.HasValue ? pageIndex.Value : 1; ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0); return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20)); } 这个意思是以二十也一页作为当前页. 在你要分页的页面的下面加入调用: @Html.Action("PageIndex", "Product", new { action = "Index", controller = "Log", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
按照如上操作就大功告成了!
第二: 有数据提交的分页功能的实现:
第一步,建立一个 Controller命名为PageIndex的空控制器,自定义一个方法如下:
public ActionResult PageIndexKey(int currentPage, int pageCount)
{
ViewBag.PageCount = pageCount;//从操作中获取总数据页数将传入分页视图页面
ViewBag.CurrentPage = currentPage;//从操作中获取当前页数将传入分页视图页面
return PartialView();
} 第二步:在要分页的页面Index上添加视图选择分布视图,代码如下 <script>
$(function () {
$("#pageingByForm a").click(function (event) {
$("#pageIndex").val($(this).attr("pageIndex"));
//$(this).parent("Form").submit();
document.getElementsByTagName("Form").item(0).submit();
event.preventDefault();
});
});
</script>
@Html.Hidden("pageIndex")
<div id="pageingByForm">
@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
{
<span>当前没有数据</span>
}
else
{
if (ViewBag.CurrentPage <= 10)
{
<span><a pageindex="1" href="#">首页</a>|</span>
}
else
{
<span><a pageindex="1" href="#">首页</a>|</span>
<span><a pageIndex="@(ViewBag.CurrentPage - 10)" href="#">...</a>|</span>
}
for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
{
if (i <= 0)
{
continue;
}
if (i > ViewBag.PageCount)
{
break;
}
<span><a pageIndex="@i" href="#">第 @i 页</a>|</span>
}
if (ViewBag.CurrentPage >1)
{
<span><a pageIndex="@(ViewBag.CurrentPage - 1)" href="#">上一页</a>|</span>
}
if (ViewBag.PageCount > ViewBag.CurrentPage)
{
<span><a pageIndex="@(ViewBag.CurrentPage + 1)" href="#">下一页</a></span>
}
if (ViewBag.CurrentPage >= ViewBag.PageCount - 10)
{
}
else
{
<span><a pageIndex="@(ViewBag.CurrentPage + 10)" href="#">...</a>|</span>
<span><a pageIndex="@ViewBag.PageCount" href="#">尾 页</a></span>
}
<span style="padding-left: 20px">当前页数: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 页
</span>
}
</div>
在操纵中(Index)的部分代码:
public ViewResult Index(int? pageIndex ,string search) { int pageInd = pageIndex.HasValue ? pageIndex.Value : 1; ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0); return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20)); }
这个意思是以二十也一页作为当前页.
在视图页中需要添加如下代码:@using (Html.BeginForm())
{视图页中的所有代码如: 性别: @Html.TextBox("sex") 这个是指按性别进行过分页 <input type="submit" value="查询" /> 最后调用@Html.Action("PageIndexKey", "PageIndex", new { pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })} 其他的和第一中相符 。
示例:
List<string> s = new List<string>();
s.Add("张军");
//后面再添加四十条
ViewBag.PageCount = (int)Math.Ceiling(s.Count() / 20.0);
return View(s.Skip((pageInd - 1) * 20).Take(20)); 在试图页的调用是:@Html.Action("PageIndex", "PageIndex", new { action = "Index", controller = "LogController", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
- SPA项目开发动态树、数据表格、分页功能
SPA项目开发 1.修改左侧动态树 LeftNav.vue <template> <el-menu router :" class="el-menu-vertic ...
- easyui combogrid下拉表格的分页/按键/动态搜索
作者:xfl4629712 < easyui combogrid下拉表格的分页/按键/动态搜索 > 需求: 1.下拉框下拉时出现表格: 2.表格带分页功能: 3.可以使用向上键.向下 ...
- 如何用angularjs制作一个完整的表格之二__表格分页功能
接上一次,这次主要介绍表格分页功能,由于项目需要这个案例是关于前端分页的方式,现在很少会这么用了,但如有需要可以参考其中的思路 html: 1.通过UL来展示页标,其中每个页标的li是通过异步加载从获 ...
- Django中使用JS通过DataTable实现表格前端分页,每页显示页数,搜索等功能
Django架构中自带了后端分页的技术,通过Paginator进行分页,前端点击按钮提交后台进行页面切换. 优缺点:后端分页对于数据量大的场景有其优势,但页面切换比较慢. 后端分页python3代码如 ...
- ASP.NET--Repeater控件分页功能实现
这两天由于‘销售渠道’系统需要实现新功能,开发了三个页面,三个界面功能大致相同. 功能:分页显示特定sql查询结果,点击上一页下一页均可显示.单击某记录可以选定修改某特定字段<DropDownL ...
- Flutter 分页功能表格控件
老孟导读:前2天有读者问到是否有带分页功能的表格控件,今天分页功能的表格控件详细解析来来. PaginatedDataTable PaginatedDataTable是一个带分页功能的DataTabl ...
- ASP.NET MVC3的学习
ASP.NET MVC第一次课(2013-12-25晚学完) 1.ASP.NET MVC 的特点 分离任务 可扩展 强大的URL重写(路由)机制 ...
- Log4Net异常日志记录在asp.net mvc3.0的应用
前言 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是简单的介绍如何在Visual ...
- ASP.NET MVC3 Razor 调试与预加载
目录(?)[-] 获取服务器信息 FormsAuthenticationSlidingExpiration 属性 MVC3预加载 在ASP.NET MVC3开发中,调试中怎么也是不可缺少的,那对于 ...
随机推荐
- hadoop是什么
Hadoop一直是我想学习的技术,正巧最近项目组要做电子商城,我就开始研究Hadoop,虽然最后鉴定Hadoop不适用我们的项目,但是我会继续研究下去,技多不压身. <Hadoop基础教程> ...
- 前端基础之 url src href
在实际使用中有时候会犯糊涂,因此来认真了解下这三者的具体用法. url 是资源定位器,是一种数据类型,和长度,颜色等属性并列.在写img的路径的时,如<img src="http:// ...
- 写入文件(txt格式)
#region 写入文件 /// <summary> /// 写入文件 /// </summary> /// <param ...
- SSO单点登录Spring-Security & CAS使用手册
1.1概述 1.1.1单点登录介绍 单点登录(Single Sign On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决方案之一, SSO 使得在多个应用系统中,用户只需要登录一次就可 ...
- SunRay4(新蕾4) 定时自动关机方案, Linux后台自动任务crontab实践
目录: 需求和思路分析 具体实现步骤 理解Crontab Crontab具体参数详细说明 最近碰到一个想要实现定时自动关机的功能,关机的指令无非就是: shutdown -h time 调用openw ...
- Tips collection of iOS development
<转>UITableView当数据很少的时候,去掉多余的cell分割线 在tableView初始化的时候 UIView *v = [[UIViewalloc] initWithFram ...
- selenium定位元素(本内容从https://my.oschina.net/flashsword/blog/147334处转载)
注明:本内容从https://my.oschina.net/flashsword/blog/147334处转载. 在使用selenium webdriver进行元素定位时,通常使用findElemen ...
- hiho #1329 : 平衡树·Splay
#1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...
- LayoutInflater的使用
在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater.LayoutInflater在Android中是“扩展”的意思,作用 ...
- [转] - 在mac的终端中使用sublime打开文件
在mac的终端中使用sublime打开文件 使用sublime提供的命令行工具.这个命令行工具位于 /Applications/Sublime\ Text\ 2.app/Contents/Shared ...