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 6555869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:52:22+00:00 2026-05-25T12:52:22+00:00

I have made a label extension with an additional parameter but when I use

  • 0

I have made a label extension with an additional parameter but when I use ViewData.ModelMetadata.DisplayName I don’t get the display name from the Diaplsy attribute on my model?

My model has a username like this

    [Display(Name = "User name")]
    public string UserName { get; set; }

and is used like this

@Html.LabelFor(m => m.SetupModel.UserName, @<span>No fancy characters mmkay?</span>)

with this html helper

    public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex, Func<object, HelperResult> template, string labelText = null) {
        var htmlFieldName = ExpressionHelper.GetExpressionText(ex);
        var metadata = htmlHelper.ViewData.ModelMetadata;
        string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
        if (String.IsNullOrEmpty(resolvedLabelText)) {
            return MvcHtmlString.Empty;
        }

        var tag = new TagBuilder("label");
        tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
        tag.InnerHtml = string.Format(
            "{0} {1}",
            resolvedLabelText,
            template(null).ToHtmlString()
        );
        return MvcHtmlString.Create(tag.ToString());
    }

But the result is UserNameNo fancy characters mmkay? when I thought it should show the name set in the display attribute?

  • 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-25T12:52:22+00:00Added an answer on May 25, 2026 at 12:52 pm

    I missed the fact that you have different objects inside your viewmodel when I posted my previous answer. The following is just to illustrate the idea, there should be a better way to do this.

    This is the trick:

    var metadata = ModelMetadata.FromLambdaExpression(ex, htmlHelper.ViewData);
    var resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
    

    string resolvedLabelText = "";
    if (htmlFieldName.Contains('.'))
    {
        var model = htmlFieldName.Split('.');
    
        var metadata = htmlHelper.ViewData.ModelMetadata.Properties.FirstOrDefault(p => p.PropertyName == model[0] );
        if (metadata != null)
        {
            metadata = metadata.Properties.FirstOrDefault(p => p.PropertyName == model[1]);
            resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
        }
        else
            resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
    }
    else 
    {
        var metadata = htmlHelper.ViewData.ModelMetadata.Properties.First(p => p.PropertyName == htmlFieldName);
        resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
    }
    

    The above will not work for SomeObject.SomeObject.Propery for instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have made the following item renderer in mxml, but when I use
Hi I have made this function that takes a table and prepare the label
I have made a simple plugin with a form but it won't post when
net webdeveloper and usually don't make any win32 apps. but now i have to.
I need something like TextBox like Label for RadioButton content. I have made all
I have made a program with a scroll pane, but it is not working.
With git log , I get a list of commits that I have made
I have made a SVG image, or more like mini application, for viewing graphs
I have made some code which exports some details of a journal article to
I have made a custom UserControl i Vb.net (windows application). How can I add

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.