简体   繁体   English

Html.DisplayNameFor背后的逻辑是什么

[英]What is the logic behind Html.DisplayNameFor

Today I'm noticed strange behavior in scaffolded razor views, which have IEnumerable<Item> model. 今天,我注意到具有IEnumerable<Item>模型的脚手架剃刀视图中的异常行为。

Even if model is a IEnumerable it's use single item notation for the table header 即使模型是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>
    }
...

How it's itself 'unpack' collection to a single items and if it's so clever, what else it could do? 它本身如何将收藏“拆包”为单个物品,如果它是如此聪明,它还能做什么?

It's using this overload: 它正在使用此重载:

public static MvcHtmlString DisplayNameFor<TModel, TValue>(
    this HtmlHelper<IEnumerable<TModel>> html,
    Expression<Func<TModel, TValue>> expression
)

(see MSDN ) (请参阅MSDN

You can use the extension method on an IEnumerable<T> , while the lambda uses just the T to get at the property. 您可以在IEnumerable<T>上使用扩展方法,而lambda仅使用T来获取属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM