I am trying to override the caption generated by the two MVC 3 Razor extensions below. I would like to show “Is Enabled” instead of “IsEnabled”.
@Html.DisplayFor(modelItem => item.IsEnabled)
and
@Html.LabelFor(model => model.IsEnabled)
My property is decorated as follows:
[DisplayName("Is Enabled")]
public bool IsEnabled { get; set; }
The LabelFor method seems to respect the DisplayName attribute; it works just fine. However, the DisplayFor method does not pick up the DisplayName attribute.
Any ideas on how I can override the caption of a property for both Razor methods?
Thanks!
You’re mixing title and content (or name of a property and its value)
DisplayForis for the content (value of the property of the instance), so shows true or false (or a checkbox checked or not) for a boolean.LabelForis… a label (kind of title), so it will show the name of the property (IsEnabled if you don’t put a DisplayName attribute), and the value of the DisplayName attribute if it exists