How to specify a property with a name - in the name

I am using ASP.NET MVC along with jQueryMobile in a web application. I want to create a link:

<a href="/Whatever/Previous" data-role="button" data-icon="arrow-l">Previous</a> 

I have a helper extension method that allows me to do:

 <%= Html.ActionLink<WhateverController>(c => c.Previous(), "Previous", new { data-role = "button", data-icon="arrow-l" } ) %> 

With the exception of data-role and data-icon not valid as property names in C #. Using @data-role does not work either.

Is there any syntax to get around this? Or am I stuck in creating a more specialized helper that knows the correct attribute names.

+4
source share
3 answers

You can use IDictionary<string, object> instead of an anonymous object:

 Html.ActionLink<WhateverController>(c => c.Previous(), "Previous", new Dictionary<string, object> { { "data-role", "button" }, { "data-icon", "arrow-l"} }) 
+8
source

In addition to the svick answer, we made a neat change in ASP.NET MVC 3, where underlined properties in them will automatically have underscores converted to dashes.

So, if you have code like this:

 <%= Html.ActionLink<WhateverController>(c => c.Previous(), "Previous", new { data_role = "button", data_icon="arrow-l") %> 

It displays markup with a dash:

 <a href="/Whatever/Previous" data-role="button" data-icon="arrow-l">Previous</a> 
+8
source

Since this character is also a subtraction / unary minus operator, you cannot use it in an identifier. I think a custom helper method is probably the best choice.

+1
source

All Articles