MVC:传入字典的模型项是X类型,但是这个字典需要X类型的模型项
我首先使用 EF 模型并在 MVC 中使用视图模型,我遇到此错误问题:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[NKI3.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[NKI3.ViewModels.IndexCreateViewModel]'.
这是我的视图模型:
public class IndexCreateViewModel
{
public string QuestionText { get; set; }
public string Sname { get; set; }
public string Cname {get;set;}
}
这是我控制器中的操作:
public ActionResult Index()
{
var Q = db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
return View(Q.ToList());
}
这是我的强类型视图:
@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
QuestionText
</th>
<th>
Sname
</th>
<th>
Cname
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.QuestionText)
</td>
<td>
@Html.DisplayFor(modelItem => item.Sname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cname)
</td>
</tr>
}
</table>
我似乎找不到此问题的解决方案,请执行以下操作:我必须在控制器操作索引中返回我的视图模型?任何帮助表示赞赏。
提前致谢!
此致!
I am using EF model first and using viewmodels in MVC, I have problems with this error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[NKI3.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[NKI3.ViewModels.IndexCreateViewModel]'.
This is my viewmodel:
public class IndexCreateViewModel
{
public string QuestionText { get; set; }
public string Sname { get; set; }
public string Cname {get;set;}
}
This is my Action in my controller:
public ActionResult Index()
{
var Q = db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
return View(Q.ToList());
}
Here is my strongly typed view:
@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
QuestionText
</th>
<th>
Sname
</th>
<th>
Cname
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.QuestionText)
</td>
<td>
@Html.DisplayFor(modelItem => item.Sname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cname)
</td>
</tr>
}
</table>
I dont seem to find the solution for this, do I have to return my viewmodel inside the controller action index? Any help is appreciated.
Thanks in advance!
Best Regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅:下面的注释
返回 Enumerable 有帮助吗?
编辑:是的,我知道我的第一个镜头是错误的!...没有注意到你的观点想要一个
我同意其他人关于转换为匹配类型所需的返回值...
或调整视图中的预期类型...
也许您可以利用像 automapper (http://automapper.org/) 这样的映射框架来帮助您解决域对象和视图模型之间的映射代码。
这取决于你 - 无需进一步投票......
See: note down below
Would it help to return Enumerable?
EDIT: Yeah, I know my first shot was wrong!... did not noticed that your view wants a
I agree with the others about converting to the required return value to the matching type...
or adjust the expecting type in the view...
maybe you can take advantage of mapping framworks like automapper (http://automapper.org/) to help you to solve to mapping code between domain objects and viewmodels.
it's up to you - No need for further down voting...
是的 - 该操作应该返回与视图文件第一行声明的相同类型的模型。
将视图文件中的第一行更改为:
并将操作代码 (
Index()
) 更改为将返回IndexCreateViewModel
类型的对象的代码。Yes - the action should return a model of the same type declared on the first line of your view file.
Change your first line in the view file to:
and change your action code (
Index()
) to a code that will return an object of typeIndexCreateViewModel
.返回的类型
db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
看起来是
IQueryable;
您需要将该结构(您的数据模型)转换为
IEnumerable
如果您的应用中还没有帮助程序, 如果要在它们之间进行转换,那么您需要编写一个。
The type returned by
db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
looks to be
IQueryable<NKI3.Models.Question>
You need to convert that structure (your data model) to an
IEnumerable<IndexCreateViewModel>
If there isn't already a helper in your app to transform between them then you'll need to write one.