I am trying to use a display template (Pet.cshtml), which I have placed in ~/Views/Shared/DisplayTemplates
, as per convention.
The Index action gets the IEnumerable
and passes it to Index.cshtml
, which passes it along to _PetTablePartial
. So far, so good. However, when Html.DisplayForModel
is called, I get this error:
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]'.
But I (think) I can clearly see that the model item is in fact an IEnumerable. What am I doing wrong?
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>
...