How to change the display name for LabelFor in razor in mvc3?

Sham picture Sham · Jul 25, 2012 · Viewed 125.5k times · Source

In razor engine I have used LabelFor helper method to display the name

But the display name is seems to be not good to display. so i need to change my display name how to do it....

@Html.LabelFor(model => model.SomekingStatus, new { @class = "control-label"}) 

Answer

Darin Dimitrov picture Darin Dimitrov · Jul 25, 2012

You could decorate your view model property with the [DisplayName] attribute and specify the text to be used:

[DisplayName("foo bar")]
public string SomekingStatus { get; set; }

Or use another overload of the LabelFor helper which allows you to specify the text:

@Html.LabelFor(model => model.SomekingStatus, "foo bar")

And, no, you cannot specify a class name in MVC3 as you tried to do, as the LabelFor helper doesn't support that. However, this would work in MVC4 or 5.