0

Let's say, I've the following class definition:

public class Person
{
  [DisplayName("First Name")]
  public string FirstName { get; set; }
  [DisplayName("Last Name")]
  public string LastName { get; set; }
}

And, I'd like to use LabelFor(x =>x.FirstName), and so on. Unfortunately, the page inherits from

IEnumerable<T>

so there's no way to use lambda expression. Is there any workarround? Or, I do have to use the Label(String) version?

Thanks for helping

Richard77
  • 20,343
  • 46
  • 150
  • 252

2 Answers2

1
@Html.LabelFor(model => model.FirstOrDefault().LastName)

This will allow you to handle when the list is empty... very suitable for column headers. All you should check is for the cases when the list is null.

Romias
  • 13,783
  • 7
  • 56
  • 85
0

Try this -

@Html.LabelFor(model => model.**First()**.LastName)
akjoshi
  • 15,374
  • 13
  • 103
  • 121