Search code examples
c#pagedlistasp.net-core-mvc-2.0

@Html.PagedListPager can't be found


I have a ASP.NET.CORE 2.0 application and I want to add paging to my site. I started with Troy Goode PagedList but I got the same error and I went to try the X version and still got the same results.

Reference to type 'HtmlString' claims it is defined in 'System.Web', but it could not be found
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page }))

I can't even find in intellisense the method @Html.PagedListPager UPDATE: It doesn't work on a brand new empty project

@using X.PagedList.Mvc; @*import this so we get our HTML Helper*@
@using X.PagedList; 
@model IPagedList<StecajneObjave.Models.ViewModels.ObjavaViewModel>



<p>
     <a asp-action="Create">Create New</a>
</p>
    <table class="table">
   <tbody>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.DatumObjave)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NazivStecajnogDuznika)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.OIBStecajnogDuznika)
        </td>

        <td>
            @Html.ActionLink("Delaji", "Details", new { id = item.Id }) 
        </td>
    </tr>
}
   </tbody>
  </table>

<h3>Minimal Paging Control w/ Page Count Text</h3>
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page }))

I have tried reinstalling nuget packages, cleaning and rebuilding the solution but nothing seems to work. What am I missing here?


Solution

  • First, you have an extra conversion over here:

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

    This is because your model is already an IPagedList:

    @model IPagedList<StecajneObjave.Models.ViewModels.ObjavaViewModel>
    

    So all you have to do in the paged list it:

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

    Second, You are passing Url.Action but forget a parameter:

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

    When it should be:

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

    EDIT:

    See the reference for asp .net core 2 HtmlString, it is different then Asp.Net MVC:

    HtmlString documantation