DisplayFor忽略html属性的原因是什么?

Mat*_*att 4 asp.net-mvc razor

当试图使用它

@Html.DisplayFor(m => m.someField, new { htmlAttributes = "class = someclass" })
Run Code Online (Sandbox Code Playgroud)

该字段在渲染时不包含类someclass.我认为它是因为它只是写出文本而没有span元素或任何设置类的东西.

我真的想设置id,以便稍后我可以通过js更新它.

Mat*_*att 7

上课需要 @class

此评论有效

DisplayFor(),没有任何HTML属性,因此你无法做到.相反,还有其他方法可以在DIV/SPAN中使用ur class渲染数据. - Nick Oct 16 11:15

此评论也有效

我认为它应该是@ Html.DisplayFor(m => m.someField,new {@ class ="someClass"}) - Michel Oct 16 at 11:30