views:

299

answers:

2

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 now user-friendly. I want it to be "First Name".

How do I achieve that?

Thanks.

+7  A: 

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.

Nathan Taylor
Well, that looks logical :-) thanks!
Shay Friedman
You bet. Good luck!
Nathan Taylor
A: 

This should work

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