Using PagedList with a viewmodel MVC 3
Asked Answered
I

1

5

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>
Irremissible answered 8/9, 2011 at 11:38 Comment(3)
You are not giving enough info nor have you supplied a crystal ball. What does the view model look like? The display template? It seems wrong that the PagedList should be specialised to SiteAdminListModel. Do you understand what PagedList<> means?Firstling
@awrigley, Hi my apologies for that i didnt include the viewmodel code right away as often i find when a post is too in depth it doesnt get a response, i have updated the post with the viewmodel code, i have been following the sorting and paging tutorial on the MVC site and their code uses PagedList on the view declaration but they dont use a viewmodel.Irremissible
What's the code for PagedList look like? What's the code for Customer.ToPagedList? Where is the exception being thrown?Oedipus
C
7

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

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

Crimmer answered 8/9, 2011 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.