[英]Get cell DisplayName from strongly-typed-list cell by List only (empty list)
I have a list of "Book" (eg) as a model in view 我有“书” (例如)列表作为视图模型
@model List<Book>
I want to create a table that each column get it's header by Book's DisplayName propery: 我想创建一个表,该表的每一列均按Book的DisplayName属性获取其标题:
<tr>
<th class="text-right">
@Html.DisplayNameFor(modelItem => Model.someMetaData.lineNumber)
</th>
<th class="text-right">
@Html.DisplayNameFor(modelItem => Model.someMetaData.FirstName)
</th>
<tr>
You can use DisplayNameFor
helper method when your model is of type IEnumerable<T>
当模型的类型为
IEnumerable<T>
时,可以使用DisplayNameFor
helper方法。
@model IEnumerable<Book>
<table class="table table-responsive table-striped">
<tr>
<th>@Html.DisplayNameFor(a=>a.Id)</th>
<th>@Html.DisplayNameFor(a => a.Name)</th>
</tr>
@foreach (var b in Model)
{
<tr>
<td>@b.Id</td>
<td>@b.Name</td>
</tr>
}
</table>
This works only if your view is strongly typed to an IEnumerable<Book>
, It will not work for List<Book>
仅当您的视图强类型
IEnumerable<Book>
,此方法才有效;不适用于List<Book>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.