Sometimes it's just nice to get some free snippet code. Increases understanding if used right, speeds up coding if used wrong.

Enabling paging on a controller action
public class HomeController : Controller
    public ActionResult Index() { return View(); }

It really is as easy as that. Using the Paging attribute on the action creates a shared Paging in ViewData that can be used to page an arbitrary list.

Paging a list in the view (Razor)
@model IEnumerable<MyObject>
@foreach (MyObject in this.GetPage(Model))

this.GetPage is an extension method on the Page object that performs the pagination and sorting on the IEnumerable<>

Last edited Mar 31, 2011 at 3:32 PM by cmbijpost, version 8


No comments yet.