DisplayFor()
is used to render the template that matches the property type.
Display templates are .cshtml files inside /DisplayTemplates folder which in turn is inside a view folder (i.e. any folder from Home, Shared or even a specific controller).
An example.
If you've a String.cshtml template like this inside /Views/Shared:
@model String
@if (string.IsNullOrEmpty(Model)) {
<span>(no string)</span>
}
else {
<span>@Model</span>
}
Every time you call DisplayFor()
for a string property:
DisplayFor(model => model.MyStringProperty);
It renders the template accordingly to the string's value. You can be more specific and put /DisplayTemplates inside a specific View folder and them only calls from those views are affected by the template.
In your case you can be even more specific and call DisplayFor()
with a particular template.
Suppose you've a template for a particular property, called MyPropertyTemplate.cshtml. You would call DisplayFor()
like this:
DisplayFor(model => model.MyProperty, "MyPropertyTemplate");
And them, inside that template you can have whatever HTML attributes you want.
@model MyProperty
<span class="orangetxt strongtxt">@MyProperty.ToString()</span>
PS: When it doesn't find a template I guess it only calls model.Property.ToString()
without additional html.
FYI: EditorFor()
, for example, works in a similar way but it uses /EditorTemplates folder.