MonoRail学习笔记十四:分页功能的使用

本文介绍如何在MonoRail框架中实现高效的数据分页展示。通过使用PaginationHelper工具类,可以轻松地为数据列表添加分页功能,支持按页码及前后页切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Asp.net开发中的GridView功能比较强大,可以方便的进行分页等操作。
在MonoRail中也提供了方便的分页功能,只要使用PaginationHelper就可以了。当然,你如果是在数据库端进行分页的话就不是这篇笔记考虑的范围了。

Controller代码:
Model
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicclassUser
{
privateintid;
privatestringname,email,password,confirmation;

publicUser()
{
}

publicUser(stringname,stringemail)
{
this.name=name;
this.email=email;
}


publicintId
{
get{returnid;}
set{id=value;}
}


[ValidateNonEmpty(
"不能为空",FriendlyName="姓名")]
publicstringName
{
get{returnname;}
set{name=value;}
}


[ValidateNonEmpty,ValidateEmail]
publicstringEmail
{
get{returnemail;}
set{email=value;}
}


[ValidateNonEmpty]
publicstringPassword
{
get{returnpassword;}
set{password=value;}
}


[ValidateSameAs(
"Password")]
publicstringConfirmation
{
get{returnconfirmation;}
set{confirmation=value;}
}

}
publicvoidList()
{
IListlist
=newArrayList();

for(inti=1;i<36;i++)
{
Useruser
=newUser();
user.Id
=i;
user.Name
="永春_"+i;
user.Email
="GSpring_"+i+"@hotmail.com";
list.Add(user);
}


PropertyBag.Add(
"list1",PaginationHelper.CreatePagination(this,list,10));
}

vm中代码:
#foreach($itemin$list1)
#beforeall
<table>
<tr>
<th>ID</th>
<th>姓名</th>
<th>邮件地址</th>
</tr>
#before
<tr
#odd
Style
='color:gray'>
#even
Style='color:red'>

#each
<td>$item.Id</td>
<td>$item.Name</td>
<td>$item.Email</td>
#after
</tr>

#afterall
</table>

#nodata
没有数据
#end
<p>
当前显示$list1.FirstItem-$list1.LastItem条共$list1.TotalItems条
</p>

<p>
#if($list1.HasFirst)
$PaginationHelper.CreatePageLink(1,"第一页")
#end
#if($list1.HasPrevious)
$PaginationHelper.CreatePageLink($list1.PreviousIndex,"前一页")
#end
#if($list1.HasNext)
$PaginationHelper.CreatePageLink($list1.NextIndex,"后一页")
#end
#if($list1.HasLast)
$PaginationHelper.CreatePageLink($list1.LastIndex,"末页")
#end
<p>
#foreach($itemin[1..$list1.LastIndex])
$PaginationHelper.CreatePageLink($item,$item.toString())
#end


所有的分页相关功能几乎都在PaginationHelper中实现了,我们只要调用就可以了。
最后的效果如下:

提供了两种方式的分页,按页码和按前后页的方式,当然也可以定制自己需要的分页方式,代码很简单
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值