Solved

Add a css class with Html.RouteLink

Posted on 2008-10-31
4
2,811 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

691 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