MVC に次のモデルがあります。
public class ParentModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public IEnumerable<ChildModel> Children { get; set; }
}
親モデルのすべての子を表示したい場合は、次のようにします。
@Html.DisplayFor(m => m.Children)
その後、ChildModel.cshtml 表示テンプレートを作成すると、DisplayFor がリストを自動的に反復処理します。
IEnumerable のカスタム テンプレートを作成したい場合はどうすればよいですか?
@model IEnumerable<ChildModel>
<table>
<tr>
<th>Property 1</th>
<th>Property 2</th>
</tr>
...
</table>
モデル タイプが間違っている表示テンプレートを作成し、モデル タイプが間違っていると文句を言わずにIEnumerable<ChildModel>
呼び出すにはどうすればよいですか?@Html.DisplayFor(m => m.Children)