Using PagedList with a viewmodel MVC 3

Liam  picture Liam · Sep 8, 2011 · Viewed 24.7k times · Source

I am trying to implement IPagedList with a viewmodel, my main view declaration is

@model PagedList.PagedList<CustomerOrders.WebUI.Models.SiteAdminListViewModel>

Then i have a Display Template which has the following declaration

@model CustomerOrders.WebUI.Models.SiteAdminListViewModel

In my controller i am passing the viewmodel to the view

 return View(new SiteAdminListViewModel(customerSites.ToPagedList(pageIndex, pageSize), customers.ToPagedList(pageIndex, pageSize)));

This results in an error, "this dictionary requires a model item of type 'PagedList.PagedList'PagedList.PagedList1[CustomerOrders.WebUI.Models.SiteAdminListViewModel]'

I understand what the error is saying but im unsure how to ammend the viewmodel to incoporate the PagedList, has anyone else experienced this or can anyone point me in the right direction?

Edit////////////////

This is my viewmodel

public class SiteAdminListViewModel
{
    public IEnumerable<CustomerSite> CustomerSites { get; set; }
    public IEnumerable<Customer> Customers { get; set; }
    public Customer Cust { get; set; }
    public CustomerSite CustSite { get; set; }
    public bool HasPreviousPage { get; set; }
    public bool HasNextPage { get; set; }
    public int PageCount { get; set; }
    public int PageNumber { get; set; }

    public SiteAdminListViewModel()
    {

    }
    public SiteAdminListViewModel(IEnumerable <CustomerSite> customerSites, IEnumerable<Customer> customers)
    {
        CustomerSites = customerSites;
        Customers = customers;
    }
}

This is a snippet of the display template////////////

@model CustomerOrders.WebUI.Models.SiteAdminListViewModel
@using (Html.BeginForm())
{
    <p class="search-controls">
    Find site by postcode: @Html.TextBox("SearchString") &nbsp;
    <input type="submit" value="Search" /></p>
}

<td>
        @Model.CustSite.CustomerSiteId
    </td>
    <td>
        @Html.ActionLink(Model.Cust.CustomerName, "Edit", new {  Model.CustSite.CustomerSiteId })
    </td>
    <td>
        @Model.CustSite.AddressLine1
    </td>
     <td>
        @Model.CustSite.Town
    </td>

Answer

Priyank picture Priyank · Sep 8, 2011

Here is one example I found that uses ViewModel with PagedList.

http://czetsuya-tech.blogspot.com/2011/05/mvc3-dynamic-search-paging-using.html