Clashing with a c sharp keyword? Use the @ prefix

by robert 31. October 2008 03:32

I just hit another good question on stackoverflow.com.

Summary: The question deals with adding the class html attribute to the Html.RouteLink. MVC Html helpers  allow you to manage html attributes by using something like

<%= Html.RouteLink("Default", "Default",null, new { Class="css_class"}) %>

and this should render it as

<a class="css_classname">...

But in the case of the RouteLink, it renders it as

<a Class="css_classname">...

Not XHTML friendly.

I just re-discovered that you can get around this by using the @ prefix. I read about this when I was starting to use c# and never had to deal with it. How often do you need to name a class "class"? I suppose if one worked in education...

Anyway, the way to fix this is adding the @ prefix to class, like this:

<%= Html.RouteLink("Default", "Default",null, new { @class="css_class"}) %>

which renders correctly. I think the MVC team is still cleaning up the Html helper code and just hasn't gotten to the RouteLink yet.

On a side note: I really enjoy stackoverflow. The amount of stuff you can learn and re-learn from using this site is amazing. Specially if you are working on MVC!

Tags:

ASP.Net MVC | c#

Comments

10/31/2008 10:15:00 AM #

Trackback from DotNetKicks.com

Clashing with a c sharp keyword? Use the @ prefix

DotNetKicks.com

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Something about the author

Tag cloud

    Month List

    Page List