trx
trx

Reputation: 2157

Paginated List not working with the View with multiple model

I used the link to build the ASP.NET Core Web application to help with the Search/sort/pagination. I have the PaginatedList.cs class as suggested in the link to help with Pagination like

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

I have a View which uses multiple Model, so created a collection model like below

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

So I want the Pagination for the Inventorys tables on the Index view. So as suggested in the link I am using the the PaginatedList on the view like

@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)
    {

It throws error when I am trying to access CustomerData and Inventorys in the view like above enter image description here

'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?)

How can have pagination for the Inventorys tables as suggested in the link

Upvotes: 0

Views: 349

Answers (1)

Ali Zeinali
Ali Zeinali

Reputation: 571

At your View you defined PaginatedList as your View model and it doesn't have any property with name CustomerData or Inventorys.

you can change your code like this:

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

and

@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)
    {

Upvotes: 1

Related Questions