【问题标题】:Cannot convert ViewModel to Generic IEnumerable ViewModel无法将 ViewModel 转换为通用 IEnumerable ViewModel
【发布时间】:2015-07-18 00:10:58
【问题描述】:

我正在尝试使用打包的PagedList.MVC 来尝试强制我的数据拆分为页面。但是,我在 controller 中走了这么远,然后收到上述错误,下面给出了另一个错误:

'PagedList.PagedList.PagedList(System.Collections.Generic.IEnumerable, int, int)' 的最佳重载方法匹配有一些无效参数

但是,我认为一旦我的第一个错误得到解决,这个错误就会得到解决。

谁能帮我解决这个问题?

控制器:

public ActionResult SupplierReportSelection(int ClientID, int? SupplierID = null, int? ReviewPeriodID = null, int page = 1)
{
    ClaimsBySupplierViewModel SupplierModel = ClaimsBySupplier(ClientID, SupplierID, ReviewPeriodID);

    int pageSize = 10;
    int pageNumber = (page);

    PagedList<ClaimsBySupplierViewModel> model = new PagedList<ClaimsBySupplierViewModel>(SupplierModel, page, pageSize);
    ViewBag.client = client;

    return View("ClaimsBySupplier", model);
} 

视图模型:

public class ClaimsBySupplierViewModel : ReportViewModel {
        public IQueryable<ClaimsBySupplierReport> ReportData { get; set; }
        public IQueryable<SupplierAndClaimNumberReviewTotalsByStatusCategory> ReportTotalData { get; set; }
        public decimal? Conversion { get; set; }
    }

【问题讨论】:

  • 那么你到底想要“页面”什么呢? ReportData 还是 ReportTotalDataPagedList 仅“页面”一个集合,因此不清楚您打算在视图中显示什么。
  • 抱歉,ReportData 和 ReportTotalData 都包含我的模型的集合。我只想将结果分成页面以减少加载时间
  • 我已经更新了我的答案,您将模型作为一个整体引用,但需要指定哪个列表。
  • 我刚刚在上次之前看到了您的评论,如果您希望两个列表都显示在同一页面上但作为分页列表,只需将列表与 Linq 或在您填充列表的存储库中合并.
  • 我也不想这样做,我只想显示一个包含我的模型 ClaimsBySupplierModel 的 ClaimsBySupplierViewModel 列表。然后我想将此列表拆分为页面

标签: c# asp.net-mvc pagination


【解决方案1】:

PagedList 正在寻找一个 IEnumerable 对象,例如一个对象列表。

因为您的 SupplierModel 看起来特定于具有属性的一个对象,这就是导致您的问题的原因。

看到模型的代码后,您需要做的是引用模型的要显示的数据列表,而不仅仅是模型本身。

而不是实例化 PagedList 对象,而是返回包含数据列表的视图。

return View("ClaimsBySupplier", SupplierModel.ReportData.ToPagedList(page, pageSize);

【讨论】:

  • 感谢您的回复。但是,即使我实现了这个问题,我仍然遇到同样的问题。这是因为我需要我的模型而不是我的视图模型的分页列表,因为我只有一个视图模型,其中包含一个模型的集合
猜你喜欢
  • 1970-01-01
  • 2021-03-06
  • 2020-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 2012-08-06
相关资源
最近更新 更多