我第一次使用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