【问题标题】:Paginated List not working with the View with multiple model分页列表不适用于具有多个模型的视图
【发布时间】:2021-12-07 13:04:14
【问题描述】:

我使用link 构建了 ASP.NET Core Web 应用程序来帮助搜索/排序/分页。我有链接中建议的 PaginatedList.cs 类来帮助分页,如

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; }
}

所以我想要索引视图上的库存表的分页。因此,正如链接中所建议的那样,我在视图上使用了 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 和 Inventorys 时会引发错误

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

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

如何按照链接中的建议对库存表进行分页

【问题讨论】:

  • 您的模型是一个集合。 PaginatedList 没有属性 Inventorys。您的集合/模型中的项目确实具有该属性。循环遍历您的模型并使用 item.CustomerData.CustomerNumber
  • @lordvlad30 我正在按照链接构建它,我对 Web 应用程序开发非常陌生。你能帮我建议是否需要更改 PaginatedList 类吗?

标签: asp.net asp.net-mvc entity-framework asp.net-core entity-framework-core


【解决方案1】:

在您的 View 中,您将 PaginatedList 定义为您的 View 模型,并且它没有任何名称为 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)
    {

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 2019-04-13
    • 2017-08-07
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多