asp.net-mvc 我可以在 MVC3 中向 HTML.ActionLink 添加一个类吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5907530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:10:51  来源:igfitidea点击:

Can I add a class to an HTML.ActionLink in MVC3

asp.net-mvc

提问by Sango

I have this code and would like to add a class to the link. Is it possible to do this in MVC3?

我有这个代码,想在链接中添加一个类。是否可以在 MVC3 中做到这一点?

Html.ActionLink("Create New", "Create")

回答by Damb

Yes, you can just add another parameter with object representing css class:

是的,您可以添加另一个带有代表 css 类的对象的参数:

Html.ActionLink("Create New", "Create", CONTROLLERNAME, null, new { @class= "yourCSSclass"} )

It can be translated to:

它可以翻译成:

Html.ActionLink(link text, action name, controller name, route values object, html attributes object)

Edit:

编辑:

To add custom styles, use this:

要添加自定义样式,请使用:

Html.ActionLink(
"Create New",
"Create",
CONTROLLERNAME,
null,
new { @class= "yourCSSclass", @style= "width:100px; color: red;" }
)

回答by RPM1984

@Html.ActionLink("ClickMe",  // link text
                 "Index", // action name
                 "Home",  // controller 
                 new { id = 2131 }, // (optional) route values
                 new { @class = "someClass" }) // html attributes

回答by archil

Html.ActionLink("Create New", "Create", null, htmlAttributes: new { @class = "className" })

回答by Rhapsody

According to the documentation, this should do the trick:

根据文档,这应该可以解决问题:

Html.ActionLink("LinkText", "Action", "Controller", new { }, new {@class="css class"})

Edit:Thanks for noticing Dampe, I updated the code sample.

编辑:感谢您注意到 Dampe,我更新了代码示例。

回答by verdesmarald

You can use the ActionLink overload which takes an htmlAttributes parameter to add a class to the generated element:

您可以使用带有 htmlAttributes 参数的 ActionLink 重载将类添加到生成的元素:

Html.ActionLink("Create New", "Create", new {}, new { @class = cssClass });