首页 文章

将CSS样式应用于html帮助器

提问于
浏览
1

我有一个带有剃刀视图引擎的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" })

但是这个样式没有应用,我把它作为源代码来源代码

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

所以我需要知道:

  • 为什么会这样?

  • 如何修复我的代码?

1 回答

  • 5

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

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

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

    注意:

    • 帮助器会自动将下划线更改为属性名称的破折号 .

    • @ 符号应该添加到匿名对象中与C#中的保留字完全匹配的任何字段名称之前 .

相关问题