【问题标题】:Change from List to IEnumerable从 List 更改为 IEnumerable
【发布时间】:2016-04-16 09:41:27
【问题描述】:

我的应用程序停止并给我这个错误消息:

The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[Test.Models.GamesVM]', 
but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1
[Test.Models.Runner]'.

我的控制器:

return PartialView("PartialViewAllRunners", db.Runners.ToList());

我的观点:

@Html.Partial("~/Views/Shared/PartialViewAllRunners.cshtml", Model)

ViewPartial View 都有IEnumerable,那么有什么问题,需要改变什么?

【问题讨论】:

  • This SO post 为您提供答案。
  • 该消息是不言自明的-您将GamesVM 的集合传递给期望Runner 集合的视图(IEnumerable vs` List` 与它无关)跨度>
  • @StephenMuecke 好的,所以我添加的任何局部视图都必须是同一模型?
  • 不一定。你可以有一个包含List<GamesVM> GamesList<Runner> Runners 属性的模型,然后使用(比如)@Html.Partial("Games", model.Games) 和`@Html.Partial("Runners", model.Runners)` 将它们传递给不同的部分。但是您还没有向我们展示足够多的代码,无法确切知道您的错误是在哪里引发的
  • @StephenMuecke 所以如果我理解你的正确,我可以在同一个视图中添加来自不同控制器和模型的多个局部视图?这正是我想要做的。如果我尝试在视图中添加 ´...model.Games),Intellisense 不会给我选项!如果顶部的 Views 主模型是 View Model 会不会有问题?我必须再检查一下我的代码。如果我没有显示足够的代码,我可以添加什么来更容易地发现问题?

标签: c# asp.net-mvc


【解决方案1】:

编辑 - 将您的视图模型更改为:

@model IEnumerable

返回视图(db.modelclass.ToList());

【讨论】:

    猜你喜欢
    • 2013-06-12
    • 2012-02-24
    • 2011-06-09
    • 2011-11-28
    • 2018-08-04
    • 2012-11-29
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    相关资源
    最近更新 更多