Using MVC PagedList in Sitecore

15 juni 2017 om 00:00 by Menderes Demir - Post a comment

When you get a lot of items in your list such as news, you obviously want to use pagination. By installing the PagedList.Mvc NuGet package you can easily accomplish this. So I added it to my project and got pagination. However, when I wanted to navigate to the next page, I ran into a problem with the generated URL from:


@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))

The generated URL for the second page is /api/sitecore/News?page=2. Of course, this doesn't work and results in an error message: The resource cannot be found. In my case the URL should be /News?page=2, without /api/sitecore/.

In order to get the /api/sitecore/ part removed, I have edited the Url.Action section, which didn't help. Then I created a custom route. This changed the URL as I wanted, but the solution wasn't okay for me.

So I eventually modified the part after "page =>". The code is now:


@Html.PagedListPager(Model, page => LinkManager.GetItemUrl(Sitecore.Context.Item) + "?page=" + page)

Instead of:


@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))

The URL no longer contains the /api/sitecore/ part, and it has become a dynamic URL based on the current page.

Nieuwste