if it is a label, use proper helper for it as Nataka526 suggests
otherwise put it in a span with a class and update css for that class:
your html:
<span class="name">
@Html.DisplayFor(m => m.Name)
</span>
your css:
.name {
//custom css
}
UPDATE
Another option:
Update your Display Templates to handle a specific ViewData key:
in Views > Shared > DisplayTemplate (create this folder if you don't have it already):
add file String.cshtml:
@model string
@{
string myClass = ViewData["class"]
}
@if(string.IsNullOrEmpty(myClass))
{
@:@Model
}
else
{
<span class="@myClass">@Model</span>
}
you may need to add DisplayTemplates for other tipes as well besides string.
In the view you will write something like this:
@Html.DisplayFor(m => m.Name, new { @class= "name" })
This will add spans around it automatically.
UPDATE
Explanation:
There is an overload on Html.DisplayFor
which accepts two parameters: expression and object additionalViewData
. So the second parameter that I pass is that anonymous object additionalViewData. I create this object with property called class
Inside of the html helper I then check if there is a ViewData with a key class
and if there is, I put output inside a span with that class value.
**
updated variable name from class
to myClass
since "class" is not appropriate variable name.