您的位置:首页 > 编程语言 > ASP

Asp.net Mvc分页方法介绍

2011-02-01 23:07 543 查看
在项目用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: