The following will allows overriding the default display name, the alternative to using the below is to vandalise your model using a [DisplayName] attribute
Usage
<%= Html.LabelFor(m => m.Customer.Name.Forename, "First Name")%>
Code
namespace System.Web.Mvc.Html
{
public static class LabelExtensions
{
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string displayName)
{
return LabelHelper(html, ModelMetadata.FromLambdaExpression<TModel, TValue>(expression, html.ViewData), ExpressionHelper.GetExpressionText(expression), displayName);
}
internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string displayName)
{
string str = displayName ?? metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new char[] { '.' }).Last<string>());
if (string.IsNullOrEmpty(str))
{
return MvcHtmlString.Empty;
}
TagBuilder builder = new TagBuilder("label");
builder.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
builder.SetInnerText(str);
return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
}
}
}