Solved

Add a css class with Html.RouteLink

Posted on 2008-10-31
4
2,779 Views
Last Modified: 2013-11-08
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.
0
Comment
Question by:mattskiver
  • 2
  • 2
4 Comments
 
LVL 3

Assisted Solution

by:biodooms
biodooms earned 50 total points
ID: 22849097
I just recently watched the videos in the asp.net page about mvc. You basically wish to add an css link to a page dynamically in an mvc application correct?

Cant you just add the html css link in the view and databind it into the head section?

This is however just a guess since I also am new to the MVC development structure.

With kind regards,

Kevin Hendricks
0
 

Author Comment

by:mattskiver
ID: 22849194
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

0
 

Accepted Solution

by:
mattskiver earned 0 total points
ID: 22849583
Someone kindly answered this for me on stackoverflow.  The solution is to prefix "class" with an @ see the code snippet below
<%= Html.RouteLink("Click me!", "MyRoute", new { controller = "Home", action = "Index", Id = 12 }, new { @class = "play" }) %>

Open in new window

0
 
LVL 3

Expert Comment

by:biodooms
ID: 22849619
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question