我可以在HTML中添加一个类吗?在MVC3 ActionLink

时间:2021-08-17 05:32:54

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")

5 个解决方案

#1


123  

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;" }
)

#2


16  

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

#3


5  

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

#4


3  

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,我更新了代码示例。

#5


2  

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

您可以使用ActionLink重载,它使用htmlAttributes参数向生成的元素添加类:

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

#1


123  

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;" }
)

#2


16  

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

#3


5  

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

#4


3  

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,我更新了代码示例。

#5


2  

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

您可以使用ActionLink重载,它使用htmlAttributes参数向生成的元素添加类:

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