無廢話MVC入門教程八[MvcPager分頁控制元件的使用]
學習.NET MVC 只看在《無廢話系列》足夠了,《無廢話系列》簡單、快速、直接。
本文目標
一、能夠使用MvcPager進行分頁
本文目錄
一、MvcPager控制元件的簡單使用
二、C#擴充套件方法
一、MvcPager控制元件的簡單使用
1、新增MvcPager.dll的引用[下載]
2、Control中的方法
1 //獲取列表 2 public ActionResult List(int? id = 1) 3 { 4 List<DTO.User> userList = new List<DTO.User>(); 5 int totalCount = 0; 6 int pageIndex = id ?? 1; 7 userList = SC.Repository.User.GetList("", 2, (pageIndex - 1) * 2, out totalCount); 8 PagedList<DTO.User> mPage = userList.AsQueryable().ToPagedList(pageIndex, 2); 9 mPage.TotalItemCount = totalCount; 10 mPage.CurrentPageIndex = (int)(id ?? 1); 11 return View(mPage); 12 }
SC.Repository.User.GetList("", 2, (pageIndex - 1) * 2, out totalCount)方法為分頁方法,此處的StrUserName只是在查詢的時候一個條件而發,其他和傳統的分頁一樣如下:
1 public staticList<DTO.User> GetList(string StrUserName, int PageSize, int CurrentCount, out int TotalCount)
PagedList<DTO.User> mPage = userList.AsQueryable().ToPagedList(pageIndex, 2);
這裡用到了擴充套件方法,首先將userList呼叫Linq中的擴充套件IEnumerable介面的方法,把List<T>轉換成為IQueryable<T>,介面如下:
1 public static IQueryable<TElement> AsQueryable<TElement>(this IEnumerable<TElement> source);
再呼叫MvcPager中對IQueryable<T>的擴充套件方法轉換成PagedList<T>供View中使用,介面如下:
1 public static PagedList<T> ToPagedList<T>(this IQueryable<T> allItems, int pageIndex, int pageSize);
3、View中使用MvcPager
1 @model PagedList<SongCai8.DTO.User> 2 @using Webdiyer.WebControls.Mvc; 3 @{ 4 Layout = null; 5 } 6 <!DOCTYPE html> 7 <html> 8 <head> 9 <title>List</title> 10 </head> 11 <body> 12 @foreach (SongCai8.DTO.User user in Model) 13 { 14 @user.UserID<span>---</span>@user.UserName<span>---</span> 15 @Html.ActionLink("Edit", "Edit", new { id = user.UserID }) <span>---</span> 16 @Html.ActionLink("Details", "Details", new { id = user.UserID }) <span>---</span> 17 @Html.ActionLink("Delete", "Delete", new { id = user.UserID })<span>---</span> 18 19 <br /> 20 } 21 <br /> 22 <br /> 23 @Html.Pager(Model, new PagerOptions 24 { 25 PageIndexParameterName = "id", 26 ShowPageIndexBox = true, 27 FirstPageText = "首頁", 28 PrevPageText = "上一頁", 29 NextPageText = "下一頁", 30 LastPageText = "末頁", 31 PageIndexBoxType = PageIndexBoxType.TextBox, 32 PageIndexBoxWrapperFormatString = "請輸入頁數{0}", 33 GoButtonText = "轉到" 34 }) 35 <br /> 36 >>分頁 共有 @Model.TotalItemCount 篇留言 @Model.CurrentPageIndex/@Model.TotalPageCount 37 </body> 38 </html>
4、效果如下:
二、C#擴充套件方法
MSDN:擴充套件方法使您能夠向現有型別“新增”方法,而無需建立新的派生型別、重新編譯或以其他方式修改原始型別。 擴充套件方法是一種特殊的靜態方法,但可以像擴充套件型別上的例項方法一樣進行呼叫。
理解什麼是擴充套件方法:用靜態類及靜態方法為原有類的新增新的方法。說起來有點抽象,邊上程式碼邊解釋。
原始類:
1 public class Person 2 { 3 public string ShowName() 4 { 5 return "顯示名稱"; 6 } 7 }
我們定義了一個Person類,類裡只有一個方法ShowName()。眾所周知,程式碼如下:
1 Person person = new Person(); 2 person.ShowName();
擴充套件類:
加入“顯示密碼的”擴充套件方法(為了體現出對比性,下面程式碼稍有重複):
1 //原始類 2 public class Person 3 { 4 public string ShowName() 5 { 6 return "顯示名稱"; 7 } 8 } 9 10 //擴充套件Person的靜態類 11 public static class ExtensionMethod 12 { 13 public static string ShowPassword(this Person person) 14 { 15 return "顯示密碼"; 16 } 17 }
程式碼解釋:
ExtensionMethod中的ShowPassword引數this Person 說明要擴充套件的型別為Person類,並且要用this關鍵字修飾。在使用時這個引數是不需要傳入的,這點和我們在傳統的類中方法引數有些不同。
使用:
使用與平時我們使用在類中定義的方法相同,程式碼如下:
1 Person person = new Person(); 2 //原始方法 3 Response.Write(person.ShowName()); 4 //換行 5 Response.Write("<br />"); 6 //擴充套件方法 7 Response.Write(person.ShowPassword());
效果如下:
應用:
最常見的擴充套件方法是新增查詢功能新增到現有 System.Collections.IEnumerable 和 System.Collections.Generic.IEnumerable<T> 型別的 LINQ 標準查詢運算子。若要使用標準查詢運算子,請首先將它們輸入與 using System.Linq 名稱空間。 然後,任何實現了 IEnumerable<T> 的型別看起來都具有 GroupBy、OrderBy、Average 等例項方法。 在 IEnumerable<T> 型別的例項(如 List<T> 或 Array)後鍵入“dot”時,可以在 IntelliSense 語句完成中看到這些附加方法。