?
Solved

Add a css class with Html.RouteLink

Posted on 2008-10-31
4
Medium Priority
?
2,838 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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