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

  • Home
  • SEARCH
  • 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 8795933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:30:08+00:00 2026-06-13T23:30:08+00:00

I have a requirement to display a label whose text is determined at run-time.

  • 0

I have a requirement to display a label whose text is determined at run-time.

Previously, when the text was known at compile-time, I was able to use the DisplayName DataAnnotation to craft how my model represented its data.

As an example, I used to have this:

[DisplayName("Task Template")]
public List<KeyValuePair<int, string>> TaskTemplate { get; set; }

<%= Html.LabelFor(model => model.TaskTemplate) %>
<%= Html.DropDownListFor(model => model.TaskTemplateFlag, new SelectList(Model.TaskTemplate, "Key", "Value"))%>

but now the DisplayName needs to be determined at run-time.

According to this StackOverflow answer there is no way to do this using a DataAnnotation.

I’m left wondering what best practice is? Do I stop using LabelFor altogether? Should I just store a bunch of string properties in my model, load my values into the properties, and render using DisplayFor?

Update: I think that this is probably a good enough solution:

public string TaskTemplateLabel { get; set; }
public List<KeyValuePair<int, string>> TaskTemplate { get; set; }

//Constructor: TaskTemplateLabel = GetLabelFromDataSource();

<%= Html.LabelFor(model => model.CurrentDeviceName, Model.TaskTemplateLabel)%>
  • 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-06-13T23:30:10+00:00Added an answer on June 13, 2026 at 11:30 pm

    According to my understanding this is as easy as use an overload of the LabelFor html helper

    @Html.LabelFor(x => item.ID, item.ID.ToString())
    

    In the above example, a label will be created for the ID property and the text will actually be the value of the property

    This is the rendered result:

    <label for="item_ID">7061207d-4ad4-45dd-aada-8335b98538c3</label>
    

    Compare this to the traditional example:

    @Html.LabelFor(x => item.ID)
    

    Which renders:

    <label for="item_ID">ID</label>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement to display text where only a substring is bold. Is
I have a requirement to display a somewhat large amount of text, read only
I have a requirement to display user available time in Hours:Minutes:Seconds format from a
I have a requirement to display dynamically generated word document on the server in
currently I have a requirement to display related items in a multi level grid.
I have an MVC3 C#.Net web app. I have a requirement to display a
I have a requirement wherein I need to display on a User form, a
I have a requirement where I ask user for confirmation and also display messages.
I have an ASP ListView, and have a very simple requirement to display numbers
I have the fllowing requirement, I have a model called Task to display user

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.