ASP.NET MVC 中的数据分页(三
2009-05-13 10:14
447 查看
我們最近在開發 ASP.NET MVC 專案的過程中,除了自行研究如何有效分頁以外,也上網找了好幾套 ASP.NET MVC 分頁的解決方案,最後我們總結出一個最好用的就是這個元件 ( Paging with ASP.NET MVC )。
要利用這個元件進行資料分頁,不外乎有幾件事必須做到:
需傳入一個 page 參數到 Action 中,用以指定你目前要顯示「第幾頁」的資料。
準備傳入的資料(Model),可透過 Paging with ASP.NET MVC 元件中提供的 Extension Method 將 IList<T>, IQueryable<T>, 或 IEnumable<T> 型別的資料轉換成 IPagedList<T> 的型別,並傳入 View 中。
透過一個自訂的 Html Helper 在 View 中必須顯示「分頁導覽列」的資訊 (如下圖)
依據上面三個步驟進行修改,Action 的程式碼會變成這樣:
view plaincopy to clipboardprint?
// 分頁後每頁顯示的筆數
int defaultPageSize = 10;
// 傳入 page 參數 ( 透過 Model Binder 綁進來的 )
public ActionResult Index(int? page)
{
IQueryable<Customer> custs =
from cust in db.Customers
where cust.City == "Taiwan"
select cust;
// 計算出目前要顯示第幾頁的資料 ( 因為 page 為 Nullable<int> 型別 )
int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
// 透過 ToPagedList 這個 Extension Method 將原本的資料轉成 IPagedList<T>
return View(custs.ToPagedList(currentPageIndex, defaultPageSize));
}
然後在 View 中顯示資料的地方,透過一個自訂的 Html Helper 方法顯示分頁資訊。
首先必須先修改傳入 View 的 Model 型別
view plaincopy to clipboardprint?
<%@ Page Language="C#"
Inherits="System.Web.Mvc.ViewPage<IPagedList<Customer>>" %>
然後再宣告匯入 MvcPaging 命名空間,好讓 Html.Pager 這個 Html Helper Method 可以使用:
備註: 也可以在 web.config 設定,請參考 ASP.NET 如何預設匯入指定的命名空間(Namespace) 文章!
view plaincopy to clipboardprint?
<%@ Import Namespace="MvcPaging"%>
然後原本顯示資料的程式「完全不用改寫」,只要加上「分頁導覽列」即可:
view plaincopy to clipboardprint?
<table>
<% foreach (var item in Model) { %>
<tr>
<td><%= Html.Encode(item.ID) %></td>
<td><%= Html.Encode(item.Name) %></td>
<td><%= Html.Encode(item.Tel) %></td>
</tr>
<% } %>
</table>
<div class="pager">
<%= Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount) %>
</div>
要利用這個元件進行資料分頁,不外乎有幾件事必須做到:
需傳入一個 page 參數到 Action 中,用以指定你目前要顯示「第幾頁」的資料。
準備傳入的資料(Model),可透過 Paging with ASP.NET MVC 元件中提供的 Extension Method 將 IList<T>, IQueryable<T>, 或 IEnumable<T> 型別的資料轉換成 IPagedList<T> 的型別,並傳入 View 中。
透過一個自訂的 Html Helper 在 View 中必須顯示「分頁導覽列」的資訊 (如下圖)
依據上面三個步驟進行修改,Action 的程式碼會變成這樣:
view plaincopy to clipboardprint?
// 分頁後每頁顯示的筆數
int defaultPageSize = 10;
// 傳入 page 參數 ( 透過 Model Binder 綁進來的 )
public ActionResult Index(int? page)
{
IQueryable<Customer> custs =
from cust in db.Customers
where cust.City == "Taiwan"
select cust;
// 計算出目前要顯示第幾頁的資料 ( 因為 page 為 Nullable<int> 型別 )
int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
// 透過 ToPagedList 這個 Extension Method 將原本的資料轉成 IPagedList<T>
return View(custs.ToPagedList(currentPageIndex, defaultPageSize));
}
然後在 View 中顯示資料的地方,透過一個自訂的 Html Helper 方法顯示分頁資訊。
首先必須先修改傳入 View 的 Model 型別
view plaincopy to clipboardprint?
<%@ Page Language="C#"
Inherits="System.Web.Mvc.ViewPage<IPagedList<Customer>>" %>
然後再宣告匯入 MvcPaging 命名空間,好讓 Html.Pager 這個 Html Helper Method 可以使用:
備註: 也可以在 web.config 設定,請參考 ASP.NET 如何預設匯入指定的命名空間(Namespace) 文章!
view plaincopy to clipboardprint?
<%@ Import Namespace="MvcPaging"%>
然後原本顯示資料的程式「完全不用改寫」,只要加上「分頁導覽列」即可:
view plaincopy to clipboardprint?
<table>
<% foreach (var item in Model) { %>
<tr>
<td><%= Html.Encode(item.ID) %></td>
<td><%= Html.Encode(item.Name) %></td>
<td><%= Html.Encode(item.Tel) %></td>
</tr>
<% } %>
</table>
<div class="pager">
<%= Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount) %>
</div>
相关文章推荐
- [翻译]在ASP.NET MVC中绑定数据(包括分页和排序)
- ASP.NET MVC 2入门演练 6 —— 分页查询数据
- 一起谈.NET技术,详解ASP.NET MVC数据分页
- Asp.net MVC 中利用jquery datatables 实现数据分页显示功能
- ASP.NET MVC 3.0(十六): MVC 3.0 实例系列之表格数据的分页
- ASP.NET MVC 数据分页
- ASP.NET MVC 数据分页思想及解决方案代码
- ASP.NET MVC 中的数据分页
- asp.net mvc 中利用jquery datatables 实现数据分页显示
- 我要学ASP.NET MVC 3.0(十六): MVC 3.0 实例系列之表格数据的分页
- ASP.NET MVC 中的数据分页(二)
- 我要学ASP.NET MVC 3.0(十六): MVC 3.0 实例系列之表格数据的分页
- asp.net mvc jqgrid 同一个页面查询不同的表,jqgrid显示不同表的表头和数据并且分页
- ASP.NET MVC 数据分页思想及解决方案代码
- 【EntityFramework系列教程三,翻译】在ASP.NET MVC程序中使用EntityFramework对数据进行排序、过滤筛选以及实现分页
- ASP.NET MVC 中的数据分页(四)
- 打造通用ASP.NET数据分页控件
- ASP.NET MVC用存储过程批量添加修改数据
- asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证
- .NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)