Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I apply a CSS class to Html.ActionLink in ASP.NET MVC?

I'm building an ASP.NET MVC application, using VB.NET and I'm trying to apply a css class to a Html.ActionLink using the code:

<%=Html.ActionLink("Home", "Index", "Home", new {@class = "tab" })%> 

But when I run the code I receive the below error:

Compiler Error Message: BC30988: Type or 'With' expected.

I'm new to MVC and really haven't much of a clue what I'm doing so I can't see what's wrong there as I'm using code based of an example elsewhere.

like image 570
LiamGu Avatar asked Sep 18 '09 13:09

LiamGu


People also ask

How do I add a class to ActionLink?

If you do not want to pass in a variable you can do the following: Html. ActionLink("View Performances", "Details", "Productions", null, new {@class = "button"}) if you just want to add the class. Also, the "Productions" part of this element is not required.

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

What is HTML ActionLink ()?

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.


2 Answers

@ewomack has a great answer for C#, unless you don't need extra object values. In my case, I ended up using something similar to:

@Html.ActionLink("Delete", "DeleteList", "List", new object { }, new { @class = "delete"}) 
like image 153
adamgede Avatar answered Sep 17 '22 12:09

adamgede


In C# it also works with a null as the 4th parameter.

@Html.ActionLink( "Front Page", "Index", "Home", null, new { @class = "MenuButtons" }) 
like image 30
coding_is_fun Avatar answered Sep 21 '22 12:09

coding_is_fun