Asp.net Mvc分页方法介绍
2011-02-01 23:07
621 查看
在项目用MVC开发也很久了,今天整理了下分页的方法,写个DEMO供大家交流参考下。
首先来看页面图片效果:
下面是项目文件截图:
在mvcweb项目中有个Global.asax.cs文件中有下代码:
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Index", action = "Index", id = UrlParameter.Optional } // 参数默认值
);
在PageController.cs中,我们用:
public ActionResult Index(string id)
{
return View(new Models.Page(id));
}
红色字体部分名称必须要相同。
再来看Models目录下Page.cs的部分代码:
public Page(string para)
{
int pageSize = 10;
PageParse sm = new PageParse(para, HttpContext.Current.Request.QueryString);
int currentPage = StrToInt(sm["page"], 1);
DataSource ds = new DataSource();//初始化数据源
int totalCount = ds.objList.Count;//总记录数
var q=ds.objList.AsQueryable<ObjectData>();
if (currentPage > 0) q = q.Skip((currentPage - 1) * pageSize);
aList = q.Take(pageSize).ToList();
Paging = new Paging<ObjectData>(pageSize, currentPage, aList, totalCount, sm.PagingUrl);
}
在aspx页面上,是这样来显示分页的:
<div style="border-bottom: 1px solid #D7D7D7;">
<label>
共<em><%=Model.Paging.TotalCount %></em>条记录</label>
<div>
<%=Model.Paging.GetStr() %>
</div>
</div>
至于MvcHelper项目,大家下载源码看看就明白。源码地址:http://files.cnblogs.com/howzanh/MvcPage.zip
首先来看页面图片效果:
下面是项目文件截图:
在mvcweb项目中有个Global.asax.cs文件中有下代码:
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Index", action = "Index", id = UrlParameter.Optional } // 参数默认值
);
在PageController.cs中,我们用:
public ActionResult Index(string id)
{
return View(new Models.Page(id));
}
红色字体部分名称必须要相同。
再来看Models目录下Page.cs的部分代码:
public Page(string para)
{
int pageSize = 10;
PageParse sm = new PageParse(para, HttpContext.Current.Request.QueryString);
int currentPage = StrToInt(sm["page"], 1);
DataSource ds = new DataSource();//初始化数据源
int totalCount = ds.objList.Count;//总记录数
var q=ds.objList.AsQueryable<ObjectData>();
if (currentPage > 0) q = q.Skip((currentPage - 1) * pageSize);
aList = q.Take(pageSize).ToList();
Paging = new Paging<ObjectData>(pageSize, currentPage, aList, totalCount, sm.PagingUrl);
}
在aspx页面上,是这样来显示分页的:
<div style="border-bottom: 1px solid #D7D7D7;">
<label>
共<em><%=Model.Paging.TotalCount %></em>条记录</label>
<div>
<%=Model.Paging.GetStr() %>
</div>
</div>
至于MvcHelper项目,大家下载源码看看就明白。源码地址:http://files.cnblogs.com/howzanh/MvcPage.zip
相关文章推荐
- Asp.net Mvc分页方法介绍
- ASP.Net MVC+Data Table实现分页+排序功能的方法
- asp.net mvc 3.0 知识点整理 ----- (3).HtmlHelper(Html 辅助方法)介绍
- asp.net mvc + dwz 分页查询的详细使用方法
- dhl:asp.net mvc下用jQuery ajax方法实现无刷新分页(用户控件)& 整体页面无刷新(2010-11-13 add)
- ASP.NET MVC分页的实现方法
- ASP.Net MVC 布局页、模板页使用方法详细介绍
- Asp.Net MVC 扩展 Html.ImageFor 方法详解
- 解决非英文版VS2008在安装ASP.NET MVC framework后无加载默认模板的方法
- 基于Bootstrap的Asp.net Mvc 分页的实现(转)
- 我要学ASP.NET MVC 3.0(十八): MVC 3.0 实例系列之表格中合并排序、分页和筛选
- ASP.net MVC分页实例
- ASP.NET MVC生命周期介绍
- 将ASP.NET MVC 1.0升级到ASP.NET MVC 2的三种方法
- 转:ASP.NET MVC扩展之HtmlHelper辅助方法
- [ASP.NET AJAX]Function对象及Type类的方法介绍
- ASP.NET MVC学前篇之扩展方法、链式编程 推荐
- asp.net mvc中ckeditor+ckfinder的配置方法 (1)
- ASP.NET MVC中@Html.DropDownListFor使用方法
- ASP.NET MVC 简单的分页思想与实现