Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net-mvc

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

Html.ActionLink("Create New", "Create") 
like image 634
Sango Avatar asked May 06 '11 06:05

Sango


People also ask

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

What does HTML ActionLink do?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

How do I pass an object in ActionLink?

If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).

How do I post on ActionLink?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to submit (post) Form in ASP.Net MVC 5 Razor. Hence in order to submit (post) Form using @Html. ActionLink, a jQuery Click event handler is assigned and when the @Html.


1 Answers

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

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;" } ) 
like image 185
Damb Avatar answered Oct 13 '22 05:10

Damb