Link to home
Start Free TrialLog in
Avatar of mattskiver
mattskiverFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Add a css class with Html.RouteLink

Hi,

Does anyone know how I can add a class to the link rendered using the Html.RouteLink helper method in ASP.Net MVC, it has the htmlAttributes object as the last parameter which I assumed I would be able to use, but since class is obviously a reserved word, I cannot supply this as one of the properties on the object.
SOLUTION
Avatar of biodooms
biodooms
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mattskiver

ASKER

Hi,  

Thanks for your response.  Let me give a little more detail.  I do not want to hardcode links to my controller actions into the views so I am using the Html.RouteLink to render out the anchor tags to specific routes passing a RouteValueDictionary specifying the controller, action and parameters.  

One overload of this helper method allows you to pass an object with any html attributes you wish to decorate the anchor tag with when it is rendered.  Unfortunately I wish to add a "class" attribute, and since this is a reserved word in C# I cannot add it to the object without a compiler error! (see code snippet).  The only way round this that I can think of is to capitalise the "C" of class, but this isn't ideal as my html will no longer validate as XHTML strict, so ideally I would like to have a solution which doesn't compromise the markup.
<%= Html.RouteLink("Click me!", "MyRoute", new { controller = "Home", action = "Index", Id = 12 }, new { class = "play" }) %>

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I understand your problem but to be honest I have to less of experience using MVC to come up with an good solution.

Please reply an solution if you found one since this cached my interest.

Thank you in advanced.