Why should I use LabelFor in MVC?

Well, if you’ve decorated your models with the DisplayNameAttribute like this:

[DisplayName("First Name")]
string FirstName { get; set; }

Then you only specify what the text of the label would be in one place. You might have to create a similar label in number of places, and if you ever needed to change the label’s text it would be convenient to only do it once.

This is even more important if you want to localize your application for different languages, which you can do with the DisplayAttribute:

[Display(Name = "FirstNameLabel", ResourceType = typeof(MyResources))]
string FirstName { get; set; }

It also means you’ll have more strongly-typed views. It’s easy to fat-finger a string like "FirstName" if you have to type it a lot, but using the helper method will allow the compiler to catch such mistakes most of the time.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)