C# Html.DisplayNameFor背后的逻辑是什么
今天,我注意到脚手架razor视图中出现了奇怪的行为,这些视图具有C# Html.DisplayNameFor背后的逻辑是什么,c#,razor,asp.net-core,asp.net-core-mvc,C#,Razor,Asp.net Core,Asp.net Core Mvc,今天,我注意到脚手架razor视图中出现了奇怪的行为,这些视图具有IEnumerable模型 即使模型是一个IEnumerable,它也会对表头使用单项表示法 @model IEnumerable<Item> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.Genre) </th>
IEnumerable
模型
即使模型是一个IEnumerable
,它也会对表头使用单项表示法
@model IEnumerable<Item>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Genre)
</th>
...
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr> ... </tr>
}
...
@model IEnumerable
@DisplayNameFor(model=>model.Genre)
...
@DisplayNameFor(model=>model.Price)
@foreach(模型中的var项目)
{
...
}
...
它本身是如何将集合“解包”到单个项目的?如果它如此聪明,它还能做什么?它正在使用此重载:
public static MvcHtmlString DisplayNameFor<TModel, TValue>(
this HtmlHelper<IEnumerable<TModel>> html,
Expression<Func<TModel, TValue>> expression
)
public静态MvcHtmlString DisplayNameFor(
这个HtmlHelper html,
表情
)
(见附件)
您可以在
IEnumerable
上使用扩展方法,而lambda只使用T
获取属性。它使用此重载:
public static MvcHtmlString DisplayNameFor<TModel, TValue>(
this HtmlHelper<IEnumerable<TModel>> html,
Expression<Func<TModel, TValue>> expression
)
public静态MvcHtmlString DisplayNameFor(
这个HtmlHelper html,
表情
)
(见附件)
您可以在
IEnumerable
上使用扩展方法,而lambda只使用T
获取属性。啊,谢谢,我明白了。所以如果它是ICollection而不是IEnumerable,它将不起作用,对吗?扩展IEnumerable
和IEnumerable
接口,这样应该仍然可以工作。啊,谢谢,我明白了。所以如果它是ICollection而不是IEnumerable,它将不起作用,对吗?扩展IEnumerable
和IEnumerable
接口,使其仍能工作。