Question

I am using ASP.NET MVC along with JQueryMobile in a web app. I want to generate a link:

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

I have a helper extension method that lets me do:

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

Except data-role and data-icon are not valid as property names in C#. Using @data-role doesn't work either.

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

Was it helpful?

Solution

You should be able to use IDictionary<string, object> instead of the anonymous object:

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

OTHER TIPS

In addition to svick's response, we made a neat change in ASP.NET MVC 3 where properties that have an underscore in them will automatically have the 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 will render the markup with dashes:

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

Because that character is also the subtraction/unary minus operator you can't use it in an identifier. I think the custom helper method is probably your best bet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top