ASP.NET MVC 开发心得显示资料分页(MvcPaging)

在網頁上進行表格資料或其他顯示資料的分頁是一種十分常見的需求,以前我們有 GridView 或 DataPager 可以幫我們自動分頁,雖然到了 ASP.NET MVC 一切全部重頭來過,但我們也不用真的那麼辛苦的自己實做分頁,因為早就有人幫我們寫好程式並開放原始碼分享給這個世界了。

如果你已經體會到在 ASP.NET MVC 中妥善利用強型別(Strong Typed)特性進行開發的優點時,你將會發現搭配 Visual Studio 2008 進行專案開發的過程有多美妙。以下我先舉一個簡單的例子:

---

你可以在 Controller 中学习林俊德心得体会 定義一個 Action 方法,並在裡面先取得所有需顯示在 View 中的資料,如果你用 LINQ to SQL 的話,可以直接傳入 IQueryable 型別的物件到 View 中,當成 View 裡面使用的 Model,這樣可以享受延遲載入(Defered Loading) 的效果。
view plaincopy to clipboardprint?

    public ActionResult Index()  
    {  
        IQueryable<Customer> custs =     
            from cust in db.Customers     
            where cust.City == "Taiwan" 
            select cust;   
              
        return View(custs);  
    } 

public ActionResult Index()
{
 IQueryable<Customer> custs =  
  from cust in db.Customers  
  where cust.City == "Taiwan"
  select cust;
  
 return View(custs);
}

之後在你的 View 中宣告繼承時可透過泛型指派 IQueryable<Customer> 進去:
view plaincopy to clipboardprint?

    <%@ Page Language="C#"   
             Inherits="System.Web.Mvc.ViewPage<IQueryable<Customer>>" %> 

<%@ Page Language="C#"
         Inherits="System.Web.Mvc.ViewPage<IQueryable<Customer>>" %>

或是轉型成統一個 IEnumable<Customer> ,這也是比較常見的用法:
view plaincopy to clipboardprint?

    <%@ Page Language="C#"   
             Inherits="System.Web.Mvc.ViewPage<IEnumable<Customer>>" %> 

<%@ Page Language="C#"
         Inherits="System.Web.Mvc.ViewPage<IEnumable<Customer>>" %>

然後你就可以利用 foreach 取出所有資料並將資料顯示出來了:
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> 

<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>

這就是標準的 ASP.NET MVC 取得資料並顯示在 View 中的 Pattern。

---

我們最近在開發 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));  
    } 

// 分頁後每頁顯示的筆數
int defaultPageSize = 10;

// 傳入 page 參數 ( 透過 Model Binder 綁進來的 )

[1] [2] 下一页

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有