【问题标题】:What is the reason DisplayFor ignores html attributes?DisplayFor 忽略 html 属性的原因是什么?
【发布时间】:2012-10-06 11:11:41
【问题描述】:

当尝试使用这个时

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

该字段在呈现时不包含类 someclass。我想它是因为它只是写出文本并且没有跨度元素或任何设置类的东西。

我真的很想设置 id 以便我以后可以通过 js 更新它。

【问题讨论】:

  • DisplayFor(),没有任何 HTML 属性,因此你不能这样做。相反,还有其他方法可以使用您的类在 DIV/SPAN 中呈现数据..
  • 我觉得应该是@Html.DisplayFor(m => m.someField, new {@class="someClass"})

标签: asp.net-mvc razor


【解决方案1】:

这个解决方案对我有用(例如,当模型是 Url 时): Razor view to DisplayFor DataType.Url that opens in new window

"您可以通过在...中添加文件来覆盖默认模板"

【讨论】:

    【解决方案2】:

    类必须是@class

    此评论有效

    DisplayFor(),没有任何 HTML 属性,因此你不能这样做。 相反,还有其他方法可以在 DIV/SPAN 中呈现数据 和你的课……——尼克 10 月 16 日 11:15

    此评论也有效

    我认为应该是@Html.DisplayFor(m => m.someField, new {@class="someClass"}) – 米歇尔 10 月 16 日 11:30

    【讨论】:

    • 你也可以在课堂上使用大写字母C,而不是@class:new {Class="someClass"}
    猜你喜欢
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多