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