How to use LabelFor on a strongly typed view for a list

Viewed 13372

When I used asp.net mvc 3 scaffolding to make a list. I got a view containing a table. With the headers of that table hard coded in the view. I want to use LabelFor, so I get the l10n I need.

What I tried to do (but failed) was:

@model IEnumerable<User>
<table>
 <tr>
   <th>
      @(Html.LabelFor<User, string>(model => model.Name)) <!--This line errors-->
   </th>
 </tr>

@foreach (var item in Model) {
<tr>
 <td>
  @Html.DisplayFor(modelItem => item.Name)
 </td>
</table>

It errors with "IEnumerable does not contain a definition for Name".. etc...

How do I make this work?

4 Answers

Use DisplayNameFor instead @Html.DisplayNameFor(model =>model.Name)

Related