7

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Matt
  • 719
  • 1
  • 7
  • 18
  • http://stackoverflow.com/questions/6365633/what-is-the-html-displayfor-syntax-for – paul Oct 16 '12 at 11:05
  • 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.. – Developer Oct 16 '12 at 11:15
  • I think it should be @Html.DisplayFor(m => m.someField, new {@class="someClass"}) – Michel Oct 16 '12 at 11:30

2 Answers2

8

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

Alex
  • 2,927
  • 8
  • 37
  • 56
Matt
  • 719
  • 1
  • 7
  • 18
0

This solution worked for me (for example, when model is Url): Razor view to DisplayFor DataType.Url that opens in new window

"You could override the default template by adding a file in ..."

Community
  • 1
  • 1
Dudi
  • 3,069
  • 1
  • 27
  • 23