【问题标题】:Apply a css style to a html helper将 css 样式应用于 html 助手
【发布时间】:2015-06-22 21:08:21
【问题描述】:

我有一个带有 razor 视图引擎的 Asp.net 应用程序

我想在Html.ActionLink 中应用css 样式。所以我试图改变这一点:

 <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Espace propriétaire du terrain <span class="caret"></span></a>

到这里

 @Html.ActionLink("Espace propriétaire du terrain ", "About", "Home",null, new {  @style="class:dropdown-toggle;data-toggle:dropdown; role:button; aria-expanded:false" })

但样式未应用,我将其作为源 html 代码获取

<a href="/Home/About" style="class:dropdown-toggle;data-toggle:dropdown; role:button; aria-expanded:false">Espace propriétaire du terrain </a>

所以我需要知道:

  1. 为什么会这样?
  2. 如何修复我的代码?

【问题讨论】:

  • 必须是new { @class="dropdown-toggle", data_toggle="dropdown", role="button", aria_expanded="false" }
  • Attributes.Add("style", "class= dropdown-toggle;");不工作?
  • 可能重复,但快速查看我可以看出“@style”意味着您正在尝试设置样式属性。为了设置类属性,你必须使用“@class”
  • @larssy1 样式属性就是style="[CSS HERE]"

标签: html css .net asp.net-mvc razor


【解决方案1】:

将您的 HTML 属性指定为匿名 new {} 对象中的多个值:

@Html.ActionLink("Espace propriétaire du terrain ", "About", "Home",null, new {  @class="dropdown", data_toggle="dropdown", role="button", aria_expanded = "false" })

在您的示例中,当您需要生成的 HTML 标记中的多个属性时,您只是添加和设置 style 属性的值。

注意:

  • 助手会自动将您的下划线更改为破折号 属性名称。

  • @ 符号应添加到 与 C# 中的保留字完全匹配的匿名对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多