4

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?

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

2

If you are using a newer version of the ASP.NET MVC framework, you will have to pass an extra parameter for the 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:

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

Comments

1

Use another overload:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.