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)%>
Community
  • 1
  • 1
Sean Anderson
  • 27,963
  • 30
  • 126
  • 237
  • Have you considered using resources? – Jupaol Nov 07 '12 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?! – Sean Anderson Nov 07 '12 at 22:28
  • @jupaol What has this to do with resources? – Suhas Nov 07 '12 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? – Jupaol Nov 07 '12 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. – Sean Anderson Nov 07 '12 at 22:31
  • @Jupaol Yes. I need to display a value from the database as a label for a control. – Sean Anderson Nov 07 '12 at 22:32
  • Is the text to display coming from another property of the object? In that case you may, for example, create your own helper and put attributes on the property providing the name, so the value can be extracted at runtime... I'll try to create a code sample tomorrow for you. – Tallmaris Nov 07 '12 at 22:34
  • @Jupaol Even with resources you need to know the strings at compile time. So that does not solve your problem unless I have understood it incorrectly – Suhas Nov 07 '12 at 22:39

2 Answers2

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>
Jupaol
  • 21,107
  • 8
  • 68
  • 100
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.

Suhas
  • 7,919
  • 5
  • 34
  • 54