Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3952000
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:47:52+00:00 2026-05-20T01:47:52+00:00

I am building a custom HTML.LabelFor helper that looks like this : public static

  • 0

I am building a custom HTML.LabelFor helper that looks like this :

public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, Boolean showToolTip)
{
  var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
  ...
}

To be able to get the proper name for the property I am using the following code :

metadata.DisplayName

And on the property of the ModelView class I got :

[DisplayName("Titel")]

The problem is that I also need a description. There is an Attribute called Display and that has Name and Description but I do not see how to extract this with the metadata variable in the above code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:47:53+00:00Added an answer on May 20, 2026 at 1:47 am

    Disclaimer: The following works only with ASP.NET MVC 3 (see the update at the bottom if you are using previous versions)

    Assuming the following model:

    public class MyViewModel
    {
        [Display(Description = "some description", Name = "some name")]
        public string SomeProperty { get; set; }
    }
    

    And the following view:

    <%= Html.LabelFor(x => x.SomeProperty, true) %>
    

    Inside your custom helper you could fetch this information from the metadata:

    public static MvcHtmlString LabelFor<TModel, TValue>(
        this HtmlHelper<TModel> self, 
        Expression<Func<TModel, TValue>> expression, 
        bool showToolTip
    )
    {
        var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
        var description = metadata.Description; // will equal "some description"
        var name = metadata.DisplayName; // will equal "some name"
        // TODO: do something with the name and the description
        ...
    }
    

    Remark: Having [DisplayName("foo")] and [Display(Name = "bar")] on the same model property is redundant and the name used in the [Display] attribute has precedence in metadata.DisplayName.


    UPDATE:

    My previous answer won’t work with ASP.NET MVC 2.0. There are a couples of properties that it is not possible to fill by default with DataAnnotations in .NET 3.5, and Description is one of them. To achieve this in ASP.NET MVC 2.0 you could use a custom model metadata provider:

    public class DisplayMetaDataProvider : DataAnnotationsModelMetadataProvider
    {
        protected override ModelMetadata CreateMetadata(
            IEnumerable<Attribute> attributes, 
            Type containerType,
            Func<object> modelAccessor, 
            Type modelType, 
            string propertyName
        )
        {
            var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
    
            var displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault();
            if (displayAttribute != null)
            {
                metadata.Description = displayAttribute.Description;
                metadata.DisplayName = displayAttribute.Name;
            }
            return metadata;
        }
    }
    

    which you would register in Application_Start:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
        ModelMetadataProviders.Current = new DisplayMetaDataProvider();
    }
    

    and then the helper should work as expected:

    public static MvcHtmlString LabelFor<TModel, TValue>(
        this HtmlHelper<TModel> self, 
        Expression<Func<TModel, TValue>> expression, 
        bool showToolTip
    )
    {
        var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
        var description = metadata.Description; // will equal "some description"
        var name = metadata.DisplayName; // will equal "some name"
        // TODO: do something with the name and the description
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a custom page caching utility that uses a syntax like {Substitution:GetNonCachedData} to
I am trying to create a custom HTML Helper that encapsulates some presentation logic
I am building a custom win32 control/widget and would like to change the cursor
I'm building a custom RTE that converts user input to a homebrewn markup, now
I’m building a custom robotish machine that has 8 switches I need to interface
I'm building a youtube player with custom controls using the youtube api: http://code.google.com/apis/youtube/youtube_player_demo.html?playerType=chromeless&playerVersion=as3 I've
I'm building a custom options panel in Wordpress. One of the options I'd like
From this page (http://developer.apple.com/programs/ios/distribute.html) I've read this Custom B2B Apps You can also offer
I'm creating a custom Java Struts tag that is for building and formatting an
We're building a text templating engine out of a custom HttpModule that replaces tags

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.