Using @Html.DisplayNameFor() with PagedList

后端 未结 5 653
囚心锁ツ
囚心锁ツ 2020-12-23 13:37

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 disp

5条回答
  •  死守一世寂寞
    2020-12-23 14:22

    As an alternate solution to the accepted answer, remember that IPagedList inherits from IEnumerable. That means that you could write:

    @model IEnumerable
    

    At the beginning of the page, and just cast the model to IPagedList when needed:

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

    You can even declare the casted variable in the header, in order to use it multiple times within the page:

    @{
        ViewBag.Title = "My page title";
        var pagedlist = (IPagedList)Model;
    }
    

    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.

提交回复
热议问题