0

規則に従って、に配置した表示テンプレート (Pet.cshtml) を使用しようとしています~/Views/Shared/DisplayTemplates

インデックス アクションは を取得して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>
...
4

2 に答える 2