Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason DisplayFor ignores html attributes?

When trying to use this

@Html.DisplayFor(m => m.someField, new { htmlAttributes = "class = someclass" })

the field, when rendered does not contain class someclass. I supposed its because its just writing out the text and there isn't a span element or anything to set the class on.

I'm really looking to set the id so that I can update it via js later on.

like image 483
Matt Avatar asked Oct 16 '12 11:10

Matt


People also ask

What does HTML DisplayFor do?

DisplayFor() The DisplayFor() helper method is a strongly typed extension method. It generates a html string for the model object property specified using a lambda expression.

What is the difference between DisplayNameFor and DisplayFor in MVC?

DisplayFor displays the value for the model item and DisplayNameFor simply displays the name of the property?


1 Answers

class needs to be @class

This comment is valid

DisplayFor(), does not have any HTML attributes, thus u can't do it. Instead there are other ways of rendering the data inside a DIV/SPAN with ur class.. – Nick Oct 16 at 11:15

This comment is also valid

I think it should be @Html.DisplayFor(m => m.someField, new {@class="someClass"}) – Michel Oct 16 at 11:30

like image 81
Matt Avatar answered Oct 14 '22 08:10

Matt