开发者

How to display model name (basically LabelFor's inner text)?

I'd like to have the functionality of printing out a model's member name/display name. This is akin to using HtmlHelper<TModel>.LabelFor but having the text not wrapped in a label. Is there an extension method already built into MVC for this exact purpose? I couldn't find one so I looked at what LabelFor does with Reflector and made a LabelTextFor method:

public static string LabelTextFor<TModel, TValue>(this HtmlHelper<TModel> source, Expression<Func<TModel, TValue>> expression)
{
    var memberName = ExpressionHelper.GetExpressionText((LambdaExpression)expression);

    var metadata = ModelMetadata.FromLambdaExpression(expression, new ViewDataDictionary<TModel>());

    return metadata.DisplayName ?? metadata.PropertyName ?? memberName.Split('.').Last();
}

I'm thinking I should maybe change the name as well of开发者_如何学C this method. Thoughts are appreciated.


MVC Shipped HTML Extension is not designed for customizing looks, rather, it is designed for more 'semantic' description for your purpose.

What you want to do, is actually simpler that don't require an extra extension, but via custom templates. Please look at Brad Wilson's great article series for how to do so.


I've decided to use the method that I've created.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜