Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Css Class to ActionLink of ASP.NET MVC

In ASP.NET MVC3 This is a regular actionlink at Razor view :

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

and related html dom is:

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

So my css work correctly if I add css class to the <a> element like the following:

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

Does any one know how can I do this?

like image 912
Saeid Avatar asked Dec 16 '22 06:12

Saeid


2 Answers

You can do this to pass in html attributes.

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

You can pass other html attributes as well.

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

hope this helps

like image 182
dknaack Avatar answered Dec 27 '22 05:12

dknaack


<li>@Html.ActionLink("Home", "Index", "Home", new { @class = "myclass" })</li>
like image 31
devdigital Avatar answered Dec 27 '22 05:12

devdigital