2

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)%>
8
  • Have you considered using resources? Commented Nov 7, 2012 at 22:25
  • Sort-of. I read a few threads discussing internationalization of DisplayName strings, but their requirements seemed different. My impression was that they'd craft their resx ahead of time -- where as I would have to create the resource file on the fly as I load data from a DB. That sounded like an ugly solution, but perhaps it is the cleanest?! Commented Nov 7, 2012 at 22:28
  • @jupaol What has this to do with resources? Commented Nov 7, 2012 at 22:29
  • So if my understanding is correct what you need is display a value from the database (or any other persistence mechanism) as a label for a control. Is that right? Commented Nov 7, 2012 at 22:31
  • @Suhas The most common scenario for needing to replace hard-coded strings with 'dynamic' strings is when internationalization is needed. However, in that case, the strings are still known at compile-time and it is more of a matter of convincing the language to accept internationalized strings. Commented Nov 7, 2012 at 22:31

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

You're onto something. This isn't /quite/ what I need, but this is sufficient. I'll edit into my original question.
0

You can build you own data annotation attribute that inherits from DisplayName attribute if all the inputs required to determine the label text at runtime are available at compile time. Though I am not sure if you can inherit from DisplayName class. It may be sealed.

Another option would be to implement your own version of LabelFor extension method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.