How to display model name (mainly LabelFor for inner text)?

I would like to have the print function name / display name of a model member. This is similar to use HtmlHelper<TModel>.LabelFor, but with text not wrapped in label. Is there an extension method already built into MVC for this purpose? I could not find it, so I looked at what LabelForReflector was doing and made a method LabelTextFor:

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 think I should change the name of this method. Thoughts are evaluated.

+3
source share
2 answers

I decided to use the method that I created.

0
source

HTML- MVC , , "" .

, , , , . , Brad Wilson , .

0

Source: https://habr.com/ru/post/1780972/


All Articles