将@ Html.DisplayNameFor()与PagedList一起使用

问题描述:

我一直在尝试PagedList包来获取索引视图的分页.一切进展顺利,在控制器级别上一切正常,每页仅显示5条记录,并根据查询字符串显示适当的页面.

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.

我的问题在视图中.我将@Model更改为PagedList.IPagedList,以便可以访问Model.HasNextPage和其他属性,但是现在@Html.DisplayNameFor(model => model.ItemName)不再起作用.我收到此错误:

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>

似乎IPagedList与DisplayNameFor()不兼容.知道为什么会发生这种情况以及如何解决吗?我知道我可以手动输入列名,但是我希望以后将这些信息保留在模型中(并可以更改).

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.

您可以尝试

@Html.DisplayNameFor(model => model.FirstOrDefault().ItemName)