【发布时间】:2012-03-12 15:56:09
【问题描述】:
我首先使用 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>
我似乎没有找到解决方案,我是否必须在控制器操作索引中返回我的视图模型?任何帮助表示赞赏。
提前致谢!
最好的问候!
【问题讨论】:
-
看起来 db.Question 返回 NKI3.Models.Question 而不是您在上面显示的 ViewModel。
标签: asp.net-mvc asp.net-mvc-3 razor mvvm