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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x