简体   繁体   English

ASP.NET MVC。 如何使用DisplayNameFor来创建表标题和正文?

[英]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.

相关问题 在Asp.net MVC中为DisplayNameFor方法创建模板 - Create a Template for DisplayNameFor method in Asp.net MVC 如何使用ASP.NET MVC中的Web网格通过表标题的onclick对表的顺序进行排序? - How to sort the order of the table by the onclick of the heading of the table using web grid in ASP.NET MVC? ASP.NET MVC。 如何在没有模型的情况下渲染局部(如创建视图) - ASP.NET MVC. How render a Partial with no model (like create view) ASP.NET MVC。 如何在部分中插入SelectList? - ASP.NET MVC. How insert a SelectList inside an Partial? asp.net 核心 1.0 mvc。 从 Request.Body 获取原始内容 - asp.net core 1.0 mvc. Get raw content from Request.Body 如何使用IEnumerable空检查@ Html.DisplayNameFor-C#和ASP.NET MVC - How to null check @Html.DisplayNameFor with IEnumerable - C# & ASP.NET MVC 如何使用@HTML.DisplayNameFor 在asp.net mvc 中格式化日期时间变量 - how to format a date time variable in asp.net mvc using @HTML.DisplayNameFor ASP.NET MVC。 是否可以使用MVC防呆验证进行阵列验证? - ASP.NET MVC. Is it possible to use MVC Foolproof Validation for array validation? ASP.NET MVC。 创建模型以转换为JSON对象(具有动态名称属性) - ASP.NET MVC. Create model to convert into JSON-object (with dynamic name property) asp.net MVC。 没有重定向就不能使用void方法 - asp.net MVC. Can't use void method without redirect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM