Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a CSS class to razor @Html.DisplayFor

I want to set a new class for this part of my code, but it doesn't work correctly

@Html.DisplayFor(model => model.melk.Code, new { @class = "myClass" } )

Can anyone tell me where am I wrong?

like image 921
Ramin Avatar asked Dec 15 '22 06:12

Ramin


1 Answers

Boris showed you the correct overload, but there is also the dead simple way to just change it to:

<span class="myClass">
    @Html.DisplayFor(model => model.melk.Code)
</span>

Personally, I find it simpler, so easier to remember and maintain, even if it's not so framework-specific.

like image 67
Rumi P. Avatar answered Jan 11 '23 12:01

Rumi P.