Solved

Add a css class with Html.RouteLink

Posted on 2008-10-31
4
2,724 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now