Ehsan Ghanbari

Experience, DotNet, Solutions

How to use PagedList In MVC

PagedList.Mvc is one of many good paging and sorting packages for ASP.NET MVC, in the below simple example I'm gonna implement it for content (posts) of a blog post, install the PagedList.MVC via Nuget and take a look at this code. This is an action which returns blog posts:


       public ActionResult List(int? page)
            var posts = _blogService.GetAllBlogPosts();
            var pageNumber = page ?? 1;
            var onePageOfPosts = posts.ToPagedList(pageNumber, 10);
            ViewBag.OnePageOfPosts = onePageOfPosts;
            return View(posts);


 I just defined the OnePageOfPosts  ViewBag and onePageOfPosts.  now in the view, you have to add a line of code:


@using PagedList
@using PagedList.Mvc;
@model IEnumerable<BlogViewModel>
      ViewBag.Title = "List";
@foreach (var item in Model)
           @Html.ActionLink(item.Title, "Post", "Blog", new { postId = item.PostId, urlSlug = item.UrlSlug }, null)
           @Html.DisplayTextFor(blog => item.Body)

@Html.PagedListPager((IPagedList)ViewBag.OnePageOfPosts, page => Url.Action("List", new { page }) )


if you run this piece of code, you should have something like this in the output:


<< First < Previous 1 2 3 4 5 … Next > Last >> 


About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to use PagedList In MVC
How to query over Icollection<> of a type with linq
Redis as a cache server
Domain driven design VS model driven architecture
What's the DDD-lite?
comments powered by Disqus