1

I am using MVC3, ASP.NET 4.5, Razor.

Normally I would use, to get a standard title for my DB columns:

@Html.LabelFor(m=>m.myField)

Which would produce:

<label for="myField">MyField Title</label>

However I need to produce just "MyField Title" which will exist in some "th" elements such as:

<th>MyField Title</th>

So I need some code like:

<th>@{get Field Title}</th>

I am probably missing an obviously trick here.....

Thanks in advance.

Brandon
  • 68,708
  • 30
  • 194
  • 223
SamJolly
  • 6,347
  • 13
  • 59
  • 125

1 Answers1

7

For MVC4, you can use the DisplayNameFor helper

@Html.DisplayNameFor(m => m.MyField)

For MVC3, this helper doesn't exist, so you can use something like this answer instead to read directly from the [DisplayName] attribute (which you will have to add to your property).

Community
  • 1
  • 1
Brandon
  • 68,708
  • 30
  • 194
  • 223
  • Thanks this was incredibly useful. I used Darin's extension code, and it worked a treat. Another reason to get on and upgrade to MVC4 or 5.... Thanks again. One learns something every day. – SamJolly Feb 11 '15 at 18:50