[英]ASP.NET MVC. How use DisplayNameFor in order to create a table heading and body?
How get a property display name using DisplayNameFor()
to build a table header. 如何使用
DisplayNameFor()
获取属性显示名称以构建表头。 for instance: 例如:
@model IEnumerable<Item>
<table class="table">
<thead>
<tr>
<td>@Html.DisplayNameFor(? => ?.prop1)</td>
<td>@Html.DisplayNameFor(? => ?.prop2)</td>
<td>@Html.DisplayNameFor(? => ?.prop3)</td>
</tr>
</thead>
<tbody>
@foreach (Item item in Model) {
<tr>
<td>@Html.DisplayFor(i => item.prop1)</td>
<td>@Html.DisplayFor(i => item.prop2)</td>
<td>@Html.DisplayFor(i => item.prop3)</td>
</tr>
}
</tbody>
</table>
what should I write in the question marks? 我应该在问号上写什么?
DisplayNameFor()
has an overload that accepts IEnumerable<T>
so it simply needs to be DisplayNameFor()
有一个接受IEnumerable<T>
的重载 ,所以它只需要
<td>@Html.DisplayNameFor(m => m.prop1)</td>
Note that this only works where the the model is Enumerable<T>
(in you case @model IEnumerable<Item>
). 请注意,这仅适用于模型为
Enumerable<T>
(在您的情况下为@model IEnumerable<Item>
)。
But will not work if the model was an object containing a proeprty which was IEnumerable<T>
. 但如果模型是包含
IEnumerable<T>
的原型的对象,则无法工作 。
For example, the following will not work 例如,以下内容不起作用
<td>@Html.DisplayNameFor(m => m.MyCollection.prop1)</td>
and it would need to be 它需要
<td>@Html.DisplayNameFor(m => m.MyCollection.FirstOrDefault().prop1)</td>
which will work even if the collection contains no items. 即使集合中不包含任何项目,它也会起作用。
Side note: Under some circumstances, you may initially get a razor error, but you can ignore it. 附注:在某些情况下,您最初可能会收到剃刀错误,但您可以忽略它。 Once you run the app, that error will disappear.
运行应用程序后,该错误将消失。
You could do like this: 你可以这样做:
@model IEnumerable<Item>
<table class="table">
<thead>
<tr>
<th>@Html.DisplayNameFor(i => i.First().prop1)</th>
<th>@Html.DisplayNameFor(i => i.First().prop2)</th>
<th>@Html.DisplayNameFor(i => i.First().prop3)</th>
</tr>
</thead>
<tbody>
@foreach (Item item in Model) {
<tr>
<td>@Html.DisplayFor(i => item.prop1)</td>
<td>@Html.DisplayFor(i => item.prop2)</td>
<td>@Html.DisplayFor(i => item.prop3)</td>
</tr>
}
</tbody>
</table>
It may look like you are actually getting the first item of the IEnumerable, but you are not. 看起来你实际上是在获取IEnumerable的第一项,但事实并非如此。
Since the parameter you are passing to DisplayFor()
is just an Expression Tree, it won't execute IEnumerable's First()
method at all, internally DisplayFor()
will only check for the passed type (the parameter's type) to use reflection and build a display for it. 由于您传递给
DisplayFor()
的参数只是一个表达式树,它根本不会执行IEnumerable的First()
方法,内部DisplayFor()
只会检查传递的类型(参数的类型)以使用反射和构建它的显示器。
If you change the model to an IList<Item>
or Item[]
you can do this: 如果将模型更改为
IList<Item>
或Item[]
,则可以执行以下操作:
@model IList<Item>
<table class="table">
<thead>
<tr>
<td>@Html.DisplayNameFor(x => x[0].prop1)</td>
<td>@Html.DisplayNameFor(x => x[0].prop2)</td>
<td>@Html.DisplayNameFor(x => x[0].prop3)</td>
</tr>
</thead>
...
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.