【发布时间】:2014-03-20 18:10:47
【问题描述】:
我正在尝试使用按照惯例放置在~/Views/Shared/DisplayTemplates 中的显示模板 (Pet.cshtml)。
Index 操作获取IEnumerable 并将其传递给Index.cshtml,后者将其传递给_PetTablePartial。到现在为止还挺好。然而,当Html.DisplayForModel 被调用时,我得到了这个错误:
The model item passed into the dictionary is of type 'Pet', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Pet]'.
但我(认为)我可以清楚地看到模型项实际上是一个 IEnumerable。我做错了什么?
Controller:
public ActionResult Index()
{
return View(pet.GetPets()); // returns IEnumerable<Pet>
}
Index.cshtml:
@model IEnumerable<Pet>
{Html.RenderPartial("_PetTablePartial", Model);}
...
_PetTablePartial.cshtml:
@model IEnumerable<Pet>
@Html.DisplayForModel()
~/Shared/DisplayTemplates/Pet.cshtml:
@model IEnumerable<Pet>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
...
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-4 razor display-templates