开发者

Just text for DisplayName

How can I just开发者_JAVA百科 put the value of DisplayName attribute of model's property instead of using Html.LabelFor() in my view? Html.LabelFor() doesn't fir for me 'cause it getting me <label for=""></label> which corrupts my page's layout. So here is sample of Model's property:

[DisplayName("House number")]
        [Required(ErrorMessage = "You must specify house number")]
        [Range(1, 9999, ErrorMessage = "You have specify a wrong house number")]
        public UInt32? buildingNumber
        {
            get { return _d.buildingNumber; }
            set { _d.buildingNumber = value; }
        }

Thanks in advance, guys!


This should get the displayname from the metadata:

@ModelMetadata.FromLambdaExpression(m => m.buildingNumber, ViewData).DisplayName

Edit:

I think you can still use the statement for MVC2, just change the @:

<%: ModelMetadata.FromLambdaExpression(m => m.buildingNumber, ViewData).DisplayName %>


Borrowing heavily from http://weblogs.asp.net/imranbaloch/archive/2010/07/03/asp-net-mvc-labelfor-helper-with-htmlattributes.aspx, I created an extension method to do this. It outputs with span tags which are always safe. You could also modify this to omit the span tag entirely (eliminating two overloads since you can never take attributes in that case).

Create a class with this content, make sure your pages are importing that class' namespace, then use this in the view like Html.DisplayNameFor(x => x.Name)

public static class DisplayNameForHelper
{
    public static MvcHtmlString DisplayNameFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
    {
        return DisplayNameFor(html, expression, new RouteValueDictionary());
    }

    public static MvcHtmlString DisplayNameFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
    {
        return DisplayNameFor(html, expression, new RouteValueDictionary(htmlAttributes));
    }

    public static MvcHtmlString DisplayNameFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
    {

        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
        string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
        string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
        if (String.IsNullOrEmpty(labelText))
        {
            return MvcHtmlString.Empty;
        }
        TagBuilder tag = new TagBuilder("span");
        tag.MergeAttributes(htmlAttributes);
        tag.SetInnerText(labelText);
        return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));

    }
}


You could fetch it from the metadata:

<%
    var displayName = ModelMetadata
        .FromLambdaExpression(x => x.buildingNumber, Html.ViewData)
        .DisplayName;
%>

<%= displayName %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜