MvcContrib Grid and Pager Helpers for ASP.NET MVC Framework

Kudos to the developers who have helped create the MvcContrib CodePlex Project. We introduced the MvcContrib Project at the last Tampa ASP.NET MVC Developer Group Meeting where I showed how to easily use the strongly-typed Grid and Pager Helpers to display a list of contacts / customers. You simply use Html.Grid to display the list of items in a table and Html.Pager to display the pager bar. All-in-all it will take you about 15 minutes, if not less, to get it up and running in your ASP.NET MVC Web Application.

Here is some code using this month's homework assignment that implements the Html.Grid and Html.Pager Helpers from MvcContrib.

 

ASP.NET MVC Sample Code

 

In the Customer's Index View I simply added the Grid and Pager Helpers in MvcContrib. Notice the use of lambda's in Html.Grid to specify what properties will be displayed for each customer in the Table. I also have the ability to add custom ActionLink's, etc. Make sure you specify DoNotEncode so the links get rendered correctly in the browser. Even better is that you also get the wonderful value of intellisense!!

 

ASP.NET MVC Sample Code

 

 

IPagination and AsPagination in MvcContrib

What might also blow your mind is how easily it is to set-up pagination in the controller and view. Here is the Index Action on the Customers Controller:

 

public ActionResult Index(int? page)

{

    return View(Customer.FetchAll().AsPagination(page ?? 1,2 ));

}

 

AsPagination is an extension method that comes with MvcContrib that will help you with pagination. Here I am specifying that Customers will be paged 2 at a time for demonstration purposes. This actually returns IPagination<Customer>, so with a strongly-typed view the ViewPage must be ViewPage<IPagination<Customer>> in this case.

 

Conclusion

A lot of other cool stuff in MvcContrib, too, like custom controller factories and view engines. As luck would have it, we will be talking about creating custom Html Helpers, controller factories, and view engines at next month's meeting: ASP.NET MVC Extensibility.

 

David Hayden

 

ASP.NET MVC Book Reviews

 

posted on Thursday, June 25, 2009 10:25 AM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices