I’ve created custom Editor templates and Display templates. I’ve placed each of these types in a folder within my views folder. The folders were named either EditorTemplate or DisplayTemplate, depending upon which type of template was created.
So, now I can use EditorFor to use my custom editor template, or DisplayFor for my custom editor template.
I would like to create a custom template for a LabelFor, but I haven’t found an example of this. Would I create a folder named Labeltemplate in my Views folder and add it here?
UPDATE
The reason I was trying to extend the LabelFor was to handle a Property that is of type KeyValuePair. I want to use the Key of this property as the Label, and the value as the Display. I asked a question here about the DisplayFor to handle the Value.
My solution ended up as>
@Html.DisplayFor(m => m.MyProperty, @Model.MyProperty.Key)
Thanks,
LabelFordoesn’t use any templates. It is hardcoded in the MVC source code and it spits a<label>no matter what you do.You will have to write a custom html helper if you want to modify this behavior.
On the other hand if you want to use templates you have to use EditorFor/DisplayFor helpers. So, since a label is for displaying purposes you could use a display template and instead of using
Html.LabelFor(x => x.Foo)useHtml.DisplayFor(x => x.Foo). As far as the custom template is concerned, either you decorate theFooproperty with the[UIHint]attribute or pass it as second argument to theDisplayForhelper.UPDATE:
According to your comment you are not trying to modify the markup but only the value. That’s what the second argument of the LabelFor helper could be used for:
This creates a label which is associated with the Foo input (
forattribute of the label properly assigned) but the text shown is that of theKeyproperty on your view model.