我第一次使用EF模型和MVC使用的ViewModels,我有这个错误的问题:MVC:传递到字典的模型项目是X型的,但是这本词典需要X型的模型项目
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());
}
这里是我的斯特龙gly 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>
我似乎找不到解决方案,我必须返回我的viewmodel控制器行动指数内?任何帮助表示赞赏。
在此先感谢!
最好的问候!
它看起来像db.Question返回NKI3.Models.Question,而不是你上面显示的ViewModel。 – koregan 2012-02-22 10:19:18