What is the reason DisplayFor ignores html attributes?

When trying to use this

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

field when rendered does not contain someclass class. I assumed that this is because it just writes the text, and there is no span element or anything else to include the class.

I really want to set the id to update it via js later.

+4
source share
2 answers

class must be @class

This comment is valid.

DisplayFor () does not have any HTML attributes, so you cannot do this. Instead, there are other ways to visualize data inside a DIV / SPAN with ur lesson .. - Nick Oct 16 at 11:15

This comment is also valid.

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

+7
source

This solution worked for me (for example, when the model is Url): Razor's look at DisplayFor DataType.Url, which opens in a new window

"You can override the default template by adding a file to ..."

0
source

Source: https://habr.com/ru/post/1440023/


All Articles