I've been trying out the PagedList package to get paging for my index views. Everything was going well, and at the controller level everything is working fine, it only displays 5 records per page, and displays the appropriate page based on the querystring.
My problem is in the view. I changed the @Model to PagedList.IPagedList
so I could access the Model.HasNextPage
and other properties, but now the @Html.DisplayNameFor(model => model.ItemName)
are no longer working. I get this error:
PagedList.IPagedList<Dossier.Models.Item>' does not contain a definition for 'ItemName' and no extension method 'ItemName' accepting a first argument of type 'PagedList.IPagedList<Dossier.Models.Item>' could be found (are you missing a using directive or an assembly reference?)
Here are the relevant parts of the view:
@model PagedList.IPagedList<Dossier.Models.Item>
@using Dossier.Models.Item
...
<th>
@Html.DisplayNameFor(model => model.ItemName)
</th>
It seems IPagedList is not compatible with DisplayNameFor(). Any idea why this is happening, and how I could fix it? I know I could just manually enter the column names, but I'd like for that information to stay (and be changeable) in the model later.
As an alternate solution to the accepted answer, remember that IPagedList inherits from IEnumerable. That means that you could write:
At the beginning of the page, and just cast the model to IPagedList when needed:
You can even declare the casted variable in the header, in order to use it multiple times within the page:
This would allow you to use the DisplayNameFor helper method, and access all PagedList methods/properties, without the need for dummy elements nor calling .FirstOrDefault() for each field.
I solved the problem by creating an overload of
DisplayNameFor
that accepts aIPagedList<TModel>
.I'll be sending a pull request to PageList project to include it into the project for everyone.
You can try this
You do not need to change
@Html.DisplayNameFor
. Declare model in the view as:Just move your pager to partial view (lets name it "_Pager"):
Render the pager in your view:
Thats it.
P.S. You can create Html helper instead of partial view...
As an alternate solution you could try:
It will work even if the list is empty!