c# - 将 PagedList 与 View 模型 MVC 3 一起使用

标签 c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-viewmodel pagedlist

我正在尝试用 View 模型实现 IPagedList,我的主要 View 声明是

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

然后我有一个具有以下声明的显示模板

@model CustomerOrders.WebUI.Models.SiteAdminListViewModel

在我的 Controller 中,我将 View 模型传递给 View

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

这会导致错误,“此字典需要类型为‘PagedList.PagedList'PagedList.PagedList1[CustomerOrders.WebUI.Models.SiteAdminListViewModel]”的模型项

我明白错误在说什么,但我不确定如何修改 View 模型以合并 PagedList,是否有其他人遇到过这种情况,或者任何人都可以指出正确的方向吗?

编辑///////////////

这是我的 View 模型

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

这是显示模板的片段////////////

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

最佳答案

这是我发现的一个将 ViewModel 与 PagedList 结合使用的示例。

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

关于c# - 将 PagedList 与 View 模型 MVC 3 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7347437/

相关文章:

c# - 错误操作编辑 Controller MVC

c# - 如何从 xamrin webview 调用同一台机器上的你自己的 ASP.NET MVC 项目

asp.net-mvc - 从MVC中的资源获取[Display]属性值

c# - build 需要很长时间。怎么跟那个打?

c# - 使用 LINQ 基于三列搜索全名

asp.net-mvc - JsonSerializer - 使用 'N2' 格式序列化小数位

javascript - AngularJS:跟踪在网络应用程序中花费的时间,然后在特定时间触发事件

c# - 从部分 View 中获取当前 Controller 和操作

c# - MVC ajax 不工作

c# - 无法使用 Ninject 将依赖项注入(inject)从 Angular 服务调用的 ASP.NET Web API Controller