简体   繁体   English

将Css类添加到ASP.NET MVC的ActionLink

[英]Add Css Class to ActionLink of ASP.NET MVC

In ASP.NET MVC3 This is a regular actionlink at Razor view : 在ASP.NET MVC3中这是Razor视图中的常规动作链接:

<li>@Html.ActionLink("Home", "Index", "Home")</li>

and related html dom is: 和相关的html dom是:

<li><a href="/HomePage/">Home</a></li>

So my css work correctly if I add css class to the <a> element like the following: 所以如果我将css类添加到<a>元素,我的css正常工作,如下所示:

<li><a class="MyCssClass" href="/HomePage/">Home</a></li>

Does any one know how can I do this? 有谁知道我该怎么做?

You can do this to pass in html attributes. 您可以执行此操作以传递html属性。

@Html.ActionLink("Home", "Index", "Home", new { @class = "MyCssClass" })

You can pass other html attributes as well. 您也可以传递其他html属性。

@Html.ActionLink("Home", "Index", "Home", new { @class = "MyCssClass", otherAttributeName = 1 }) 

hope this helps 希望这可以帮助

<li>@Html.ActionLink("Home", "Index", "Home", new { @class = "myclass" })</li>

If you are using a newer version of the ASP.NET MVC framework, you will have to pass an extra parameter for the routeValues. 如果您使用的是较新版本的ASP.NET MVC框架,则必须为routeValues传递一个额外的参数。 There's no method signature overload that accepts htmlAttributes as the 4th parameter any more, so if you are not passing in routeValues, just pass in a null like this: 没有方法签名重载接受htmlAttributes作为第四个参数,所以如果你没有传入routeValues,只需传入一个null,如下所示:

@Html.ActionLink("Home", "Index", "Home", null, new { @class = "MyCssClass" })

使用另一个重载:

<li>@Html.ActionLink("Home", "Index", "Home", new { @class="YourClass" })</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM