Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Html.LabelFor to display human readable label?

I'm using ASP.NET MVC 2 and I'm struggling to understand how can I use the Html.LabelFor helpet method.

Assuming I have a model:

public class Person
{
  public string FirstName { get; set; }
}

In my view, if I write:

<%: Html.LabelFor(model => model.FirstName) %>

What I get on the page is "FirstName". But I don't want that because it's not user-friendly. I want it to be "First Name".

How do I achieve that?

Thanks.

like image 584
Shay Friedman Avatar asked Apr 28 '10 14:04

Shay Friedman


1 Answers

Just like this:

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

System.ComponentModel.DisplayNameAttribute

You should also check out System.ComponentModel.DataAnnotations for some incredibly helpful validation attributes like [Range(0, 100)], [StringLength(100)], [Required] and more.

like image 126
Nathan Taylor Avatar answered Sep 27 '22 22:09

Nathan Taylor