PagedList a List of objects in a ViewModel

I installed the PagedList.MVC Nugget package and I'm trying to make a PagedList from a list, List<MyProject.ViewModel.AddProducts>ProductsList

In my View:

@model PagedList.IPagedList<MyProject.ViewModel.AddProductsViewModel>
@using PagedList.Mvc;
    
                stuff here

                    <table class="table" align="left" style="padding-left:15px">
                        <tr>
                            
                            <th>
                                Product
                            </th>
                           
                        </tr>
                        @foreach (var item in Model.ProductsList)
                        {
                            <tr>
                                                                     
                                <td>
                                    @Html.DisplayFor(modelItem => item.Descripcion)
                                </td>
                               
                            </tr>
                        }
                    </table>
                    Página: @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) de: @Model.PageCount
                    @Html.PagedListPager(Model, page => Url.Action("Index", new
                    {
                        page,
                        sortOrder =
                        ViewBag.CurrentSort,
                        currentFilter = ViewBag.CurrentFilter
                    }))

My controller:

 public ActionResult NewSale(int? page=null)
        {
            page = (page ?? 1);
            var productspl = db.Products
              .Include(p => p.Category)
              .Include(p => p.Color).OrderBy(p => p.Description).ToPagedList((int)page, 8);           
            var saleViewModel = new AddProductsViewModel
            { 
                ProductsList = productospl,
            };
            return View(saleViewModel);
        }

My ViewModel:

namespace MyProject.ViewModel
{
    public class AddProductsViewModel
    {
          stuff
        

        public List<Productos> ListaProductos { get; set; }
     }          
}

The problem is that I can't access the ProductsList without changing this

@model PagedList.IPagedList<MyProject.ViewModel.AddProductsViewModel>

Is there a way?

Upvotes: 2

Views: 659

Answers (2)

In my ViewModel:

namespace MyProject.ViewModel
{
    public class AddProductsViewModel
    {
          stuff
        

        public List<Productos> ProductsList { get; set; }
     }          
}

Changed this:

 public List<Productos> ProductsList { get; set; }

To:

 public PagedList.IPagedList<Productos> ProductsList { get; set; }

I want to thank the user Alex for helping me get to this answer and also helping me with other issues before.

Upvotes: 1

Alex
Alex

Reputation: 35831

Try something like this:

public class ProductsPageViewModel() 
{
    public PagedList.IPagedList<MyProject.ViewModel.AddProductsViewModel> AddProductsPagedList { get; set; }
    // other properties/methods for your view go here...    
}

Then in your view:

@model ProductsPageViewModel

// other HTML here....

Página: @(Model.AddProductsPagedList.PageCount < Model.AddProductsPagedList.PageNumber ? 0 : Model.AddProductsPagedList.PageNumber) de: @Model.AddProductsPagedList.PageCount
@Html.PagedListPager(Model.AddProductsPagedList, page => Url.Action("Index", new
{
    page,
    sortOrder =
    ViewBag.CurrentSort,
    currentFilter = ViewBag.CurrentFilter
}))

Upvotes: 1

Related Questions