简单分页的实现-硬编码
2011-03-28 17:48 撞破南墙 阅读(544) 评论(0) 编辑 收藏 举报
最终效果
1控制器中代码
public ActionResult List(int id) { if (id < 1 ) {
id = 1;
}
//##3.1.3分页示例--后台代码
STOA.RichModel.STOADBContainer stoadbc = new RichModel.STOADBContainer();
IQueryable<STOA.RichModel.Role> roles =
stoadbc.Role;
int count = roles.Count();
ViewData["recordCount"] = count;
roles = roles.OrderBy(_ => _.RoleID)
.Skip((id - 1) * Base.Global.PageSize)//跳过的页数
.Take(Base.Global.PageSize);
ViewData["msg"] = roles;//
ViewData["currentPageIndex"] = id;
ViewData["pageSize"] = Base.Global.PageSize;
ViewData["pageCount"] = count / Base.Global.PageSize;//这里有问题暂时不理
return View(roles);
}
id = 1;
}
//##3.1.3分页示例--后台代码
STOA.RichModel.STOADBContainer stoadbc = new RichModel.STOADBContainer();
IQueryable<STOA.RichModel.Role> roles =
stoadbc.Role;
int count = roles.Count();
ViewData["recordCount"] = count;
roles = roles.OrderBy(_ => _.RoleID)
.Skip((id - 1) * Base.Global.PageSize)//跳过的页数
.Take(Base.Global.PageSize);
ViewData["msg"] = roles;//
ViewData["currentPageIndex"] = id;
ViewData["pageSize"] = Base.Global.PageSize;
ViewData["pageCount"] = count / Base.Global.PageSize;//这里有问题暂时不理
return View(roles);
}
2 辅助类中的代码
namespace System.Web.Mvc {
public static class PagerHelper {
/// <summary>
/// 3.1.3分页示例--辅助类代码
/// </summary>
/// <param name="currentPageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="recordCount"></param>
/// <param name="pageCount"></param>
/// <param name="modelname"></param>
/// <returns></returns>
public static string GetPager2(int currentPageIndex, int pageSize, int recordCount, int pageCount, string modelname)//(pagermode mode,string PageModel)
{
StringBuilder pagerQuery = new StringBuilder();
if (recordCount == 0) {
return "没有符合条件的信息";
}
#region =============公共分页和执行操作====================
pagerQuery.Append(" 共有<font style=\"color:blue\">" + recordCount + "</font>项");// <p>// <font style="color:#F00">
pagerQuery.Append(" 共有 " + pageCount + "页");
pagerQuery.Append(" 当前在第" + currentPageIndex + "页");
if (currentPageIndex != 1)
pagerQuery.Append("<span id='First' ><a href='" + modelname + "" + 1 + "'>首页</span></a> ");
if ((currentPageIndex - 1) > 0)//上页为0
pagerQuery.Append("<span id='Pre' ><a href='" + modelname + "" + (currentPageIndex - 1) + "'>上一页</span></a> ");
if (currentPageIndex < pageCount)//下页 满出了
pagerQuery.Append("<span id='Next' ><a href='" + modelname + "" + (currentPageIndex + 1) + "'>下一页</span></a> ");
if (currentPageIndex < pageCount)
pagerQuery.Append("<span id='Last' ><a href='" + modelname + "" + pageCount + "'>末页</span></a> ");
pagerQuery.Append(" 页次 <font style=\"color:red\">" + currentPageIndex + "</font>/" + pageCount + "页 " + pageSize + "项/页 ");
if (pageCount != 0) {
pagerQuery.Append(
" 转到<select id=\"PageIndex\" onchange=\"location.href = this.options[this.selectedIndex].value;\">");
pagerQuery.Append("<option>页面跳转</option>");
for (int i = 1; i <= pageCount; i++) { pagerQuery.Append("<option value=\"" + modelname + i + "\">第" + i + "页</option>"); }
pagerQuery.Append("</select>");
}
#endregion
return pagerQuery.ToString();
public static class PagerHelper {
/// <summary>
/// 3.1.3分页示例--辅助类代码
/// </summary>
/// <param name="currentPageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="recordCount"></param>
/// <param name="pageCount"></param>
/// <param name="modelname"></param>
/// <returns></returns>
public static string GetPager2(int currentPageIndex, int pageSize, int recordCount, int pageCount, string modelname)//(pagermode mode,string PageModel)
{
StringBuilder pagerQuery = new StringBuilder();
if (recordCount == 0) {
return "没有符合条件的信息";
}
#region =============公共分页和执行操作====================
pagerQuery.Append(" 共有<font style=\"color:blue\">" + recordCount + "</font>项");// <p>// <font style="color:#F00">
pagerQuery.Append(" 共有 " + pageCount + "页");
pagerQuery.Append(" 当前在第" + currentPageIndex + "页");
if (currentPageIndex != 1)
pagerQuery.Append("<span id='First' ><a href='" + modelname + "" + 1 + "'>首页</span></a> ");
if ((currentPageIndex - 1) > 0)//上页为0
pagerQuery.Append("<span id='Pre' ><a href='" + modelname + "" + (currentPageIndex - 1) + "'>上一页</span></a> ");
if (currentPageIndex < pageCount)//下页 满出了
pagerQuery.Append("<span id='Next' ><a href='" + modelname + "" + (currentPageIndex + 1) + "'>下一页</span></a> ");
if (currentPageIndex < pageCount)
pagerQuery.Append("<span id='Last' ><a href='" + modelname + "" + pageCount + "'>末页</span></a> ");
pagerQuery.Append(" 页次 <font style=\"color:red\">" + currentPageIndex + "</font>/" + pageCount + "页 " + pageSize + "项/页 ");
if (pageCount != 0) {
pagerQuery.Append(
" 转到<select id=\"PageIndex\" onchange=\"location.href = this.options[this.selectedIndex].value;\">");
pagerQuery.Append("<option>页面跳转</option>");
for (int i = 1; i <= pageCount; i++) { pagerQuery.Append("<option value=\"" + modelname + i + "\">第" + i + "页</option>"); }
pagerQuery.Append("</select>");
}
#endregion
return pagerQuery.ToString();
}}
3 页面中的代码
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<STOA.RichModel.Role>>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>list</title>
</head>
<body>
<p>
<%: Html.ActionLink("Create New", "Create") %>
</p>
<% IEnumerable<STOA.RichModel.Role> roles = ViewData["msg"] as IEnumerable<STOA.RichModel.Role>;%>
<table>
<tr>
<th>
</th>
<th>
Name
</th>
<th>
Description
</th>
<th>
CreateTime
</th>
<th>
State
</th>
</tr>
<% foreach (var item in roles) { %>
<tr>
<td>
<%-- <%: Html.ActionLink("Edit", "Edit", new { id=item.RoleID }) %>--%>
<a href="/Security/Role/Edit/<%=item.RoleID %>">编辑角色</a> <a href="/Security/Permission/Edit/<%=item.RoleID %>">
编辑权限组</a> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.RoleID }) %>
</td>
<td>
<%: item.Name %>
</td>
<td>
<%: item.Description %>
</td>
<td>
<%: String.Format("{0:g}", item.CreateTime) %>
</td>
<td>
<%: item.State %>
</td>
</tr>
<% } /* //##3.1.3分页示例--前台代码
* (int currentPageIndex, int pageSize, int recordCount, int pageCount, string AJaxQuest,
string PageModel) */%>
</table>
<%=( PagerHelper.GetPager2(
(int)ViewData["currentPageIndex"],(int) ViewData["pageSize"],
(int)ViewData["recordCount"], (int)ViewData["pageCount"], "/security/role/list/"))
%>
</body>
</html>
}
作者:撞破南墙
出处:http://www.cnblogs.com/facingwaller/
关于作者:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。