分页列表不适用于具有多个模型的视图

Paginated List not working with the View with multiple model

我使用 link 构建 ASP.NET 核心 Web 应用程序来帮助 Search/sort/pagination。我有 PaginatedList.cs class 中建议的 link 来帮助像

这样的分页
public class PaginatedList<T> : List<T>
{
    public int PageIndex { get; private set; }
    public int TotalPages { get; private set; }

    public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
    {
        PageIndex = pageIndex;
        TotalPages = (int)Math.Ceiling(count / (double)pageSize);

        this.AddRange(items);
    }

    public bool HasPreviousPage
    {
        get
        {
            return (PageIndex > 1);
    }}

    public bool HasNextPage
    {
        get
        {
            return (PageIndex < TotalPages);
    }}

    public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
    {
        var count = await source.CountAsync();
        var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
        return new PaginatedList<T>(items, count, pageIndex, pageSize);
    }
} 

我有一个使用多个模型的视图,因此创建了一个如下所示的集合模型

public class CustomerInventoryCollectionDataModel
{
    public Customer CustomerData { get; set; }
    public List<Inventory> Inventorys { get; set; }
}

所以我想要 Index 视图上的 Inventory 表的分页。因此,正如 link 中所建议的那样,我在

之类的视图上使用了 PaginatedList
@model PaginatedList<JAXApp.Models.CustomerInventoryCollectionDataModel>

<div>
<h4>Customer</h4>
<hr />
<dl class="row">
    <dt class="col-sm-2">
        Customer Number
    </dt>
    <dd class="col-sm-10">
        @Html.DisplayFor(model => model.CustomerData.CustomerNumber)
    </dd>
</d1>

<table class="table">
<thead>...</thead>
<tbody>
    @foreach (var item in Model.Inventorys)
    {

当我尝试在上面的视图中访问 CustomerData 和 Inventory 时抛出错误

'PaginatedList<CustomerInventoryCollectionDataModel>' does not contain a definition for 'Inventorys' and no accessible extension method 'Inventorys' accepting a first argument of type 'PaginatedList<CustomerInventoryCollectionDataModel>' could be found (are you missing a using directive or an assembly reference?)

PaginatedList<CustomerInventoryCollectionDataModel>' does not contain a definition for 'CustomerData' and no accessible extension method 'CustomerData' accepting a first argument of type 'PaginatedList<CustomerInventoryCollectionDataModel>' could be found (are you missing a using directive or an assembly reference?)

如何按照 link

中的建议对库存表进行分页

在您的视图中,您将 PaginatedList 定义为您的视图模型,并且它没有任何名称为 CustomerDataInventorys.

的 属性

您可以这样更改代码:

public class CustomerInventoryCollectionDataModel
{
    public Customer CustomerData { get; set; }
    public PaginatedList<Inventory> Inventorys { get; set; }
}

@model JAXApp.Models.CustomerInventoryCollectionDataModel

<div>
<h4>Customer</h4>
<hr />
<dl class="row">
    <dt class="col-sm-2">
        Customer Number
    </dt>
    <dd class="col-sm-10">
        @Html.DisplayFor(model => model.CustomerData.CustomerNumber)
    </dd>
</d1>

<table class="table">
<thead>...</thead>
<tbody>
    @foreach (var item in Model.Inventorys)
    {