ASP.NET MVC 中的数据分页
2009-05-13 10:12
393 查看
Updated 2009-02-04: Upgraded source and demo to ASP.NET MVC RC1
Updated 2009-01-16: Upgraded source and demo to ASP.NET MVC Beta
Updated 2008-09-09: Source and Demo now use ASP.NET MVC preview 5
On my latest gig, we used ASP.NET MVC and it was a very pleasant experience! Very clean code, clean html output and the productivity was great (which is not very common when using a technology for the first time).
Of course, since ASP.NET MVC isn’t finished yet, it leaves something to desire. At one time we needed paging and there isn’t an out-of-the-box solution for it (yet). Some googling revealed that others had already addressed this, but to be honest, I wasn’t entirely pleased with the solutions, so I grabbed the best parts and build a generic solution out of it.
I’m in a hurry, take me to the download immediately
We’re assuming that the page links point to the the current controller and current action. Alternatively, it’s possible to supply a different action:
Combining the Pager html helper and IPagedList<T> in a view page (that inherits from ViewPage<IPagedList<Product>>):
results in this:
You probably recognize the digg-style appearance of the pager. Thanks to the ASP.NET MVC sample application KIGG for inspiration :).
Internally, the pager uses RouteTable.Routes.GetVirtualPath() to render the url’s so the page url’s can be configured via routing to create nice looking url’s like for example ‘/Categories/Shoes/Page/1′ instead of ‘/Paging/ViewByCategory?name=Shoes&page=1′.
That’s it. A complete solution with demo project can be downloaded below. Feel free to leave any comments or remarks. One of the things I’m not happy about is that I failed to properly unit test the pager, but that’s for a different post.
Updated 2009-01-16: Upgraded source and demo to ASP.NET MVC Beta
Updated 2008-09-09: Source and Demo now use ASP.NET MVC preview 5
On my latest gig, we used ASP.NET MVC and it was a very pleasant experience! Very clean code, clean html output and the productivity was great (which is not very common when using a technology for the first time).
Of course, since ASP.NET MVC isn’t finished yet, it leaves something to desire. At one time we needed paging and there isn’t an out-of-the-box solution for it (yet). Some googling revealed that others had already addressed this, but to be honest, I wasn’t entirely pleased with the solutions, so I grabbed the best parts and build a generic solution out of it.
I’m in a hurry, take me to the download immediately
IPagedList<T>
First, there was the PagedList<T> that Scott Guthrie already used in one of the earliest MVC demo’s and that later appeared in improved incarnations from Rob Conery and Troy Goode. This was a nice starting point, especially because we also used LINQ To SQL as our data access layer. To provide an extra extension point, I created an IPagedList<T> interface instead of using the PagedList<T> directly.public interface IPagedList<T> : IList<T>
{
int PageCount { get; }
int TotalItemCount { get; }
int PageIndex { get; }
int PageNumber { get; }
int PageSize { get; }
bool HasPreviousPage { get; }
bool HasNextPage { get; }
bool IsFirstPage { get; }
bool IsLastPage { get; }
}
Html.Pager()
After choosing the IPagedList<T> implementation I started thinking about how I could create an HtmlHelper extension method that renders the page links. First, I made the mistake to couple the helper to the IPagedList<T> and it took me a little while to realize that a paging HTML helper doesn’t need anything to know about the underlying data source. It’s sole purpose is to generate a list of hyperlinks for pages and the only thing we need to know is the total amount of items, the page size and the current page. This is what the most basic HtmlHelper extension looks like:public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount)
We’re assuming that the page links point to the the current controller and current action. Alternatively, it’s possible to supply a different action:
public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName)
Combining the Pager html helper and IPagedList<T> in a view page (that inherits from ViewPage<IPagedList<Product>>):
<table class="grid">
<thead>
<tr>
<th>Product name</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<% foreach (var product in ViewData.Model) { %>
<tr>
<td><%= product.Name %></td>
<td><%= product.Category %></td>
</tr>
<% } %>
</tbody>
</table>
<div class="pager">
<%= Html.Pager(ViewData.Model.PageSize, ViewData.Model.PageNumber, ViewData.Model.TotalItemCount) %>
</div>
results in this:
You probably recognize the digg-style appearance of the pager. Thanks to the ASP.NET MVC sample application KIGG for inspiration :).
Internally, the pager uses RouteTable.Routes.GetVirtualPath() to render the url’s so the page url’s can be configured via routing to create nice looking url’s like for example ‘/Categories/Shoes/Page/1′ instead of ‘/Paging/ViewByCategory?name=Shoes&page=1′.
Advanced scenarios
Many scenarios where you want to use paging also use filtering. It’s possible to pass extra parameters to the Pager helper via a RouteValueDictionary or an anonymous type. This adds these parameters to the page links that are generated:<div class="pager">
<%= Html.Pager(ViewData.Model.PageSize, ViewData.Model.PageNumber, ViewData.Model.TotalItemCount, new { categoryname = ViewData["CategoryDisplayName"] } )%>
</div>
That’s it. A complete solution with demo project can be downloaded below. Feel free to leave any comments or remarks. One of the things I’m not happy about is that I failed to properly unit test the pager, but that’s for a different post.
Download
Download the paging sample solution, incl. the demo MVC web project相关文章推荐
- [翻译]在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 中利用jquery datatables 实现数据分页显示
- ASP.NET MVC 中的数据分页(二)
- 我要学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元数据设置项(自定义元数据提供程序)