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)%>