Is it possible to use LabelFor for header row in Index view

后端 未结 2 955
谎友^
谎友^ 2021-01-05 18:21

I am trying to leverage DataAnnotation values in ASP.NET MVC Index view. Interesting, code generator uses field names (eg., BlogPost) as opposed to Html.LabelFor(m =&g

2条回答
  •  余生分开走
    2021-01-05 18:54

    In MVC 4, the HtmlHelper extensions in the DisplayNameExtensions class allows the following (this is the default scaffolding of a list with column headings)

    @model IEnumerable
    
    
    
    @foreach (var item in Model) {
        
    ...
    

    This is because DisplayNameFor has overloads for both TModel and IEnumerable:

        public static MvcHtmlString DisplayNameFor
                           (this HtmlHelper> html, 
                            Expression> expression);
        public static MvcHtmlString DisplayNameFor
                           (this HtmlHelper html, 
                            Expression> expression);
    

    提交回复
    热议问题
    @Html.DisplayNameFor(model => model.Bar)
    @Html.DisplayFor(modelItem => item.Bar)