I'm using ASP.NET MVC 2 and I'm struggling to understand how can I use the Html.LabelFor helpet method.
Assumin开发者_如何学Pythong 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.
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's answer is the easiest. Another answer would be to create a custom DataAnnotationsModelMetadataProvider, which gets the property name and splits it using some string helper. Manning's ASP.NET MVC2 in Action (2nd Edition) has an example of this in Chapter 15.
This should work
<%= Html.LabelFor(model => model.FirstName) %>
加载中,请稍侯......
精彩评论