Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a property with - in the name

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.

like image 753
Rob Walker Avatar asked Dec 03 '22 10:12

Rob Walker


2 Answers

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"}
     })
like image 147
svick Avatar answered Dec 15 '22 19:12

svick


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>  
like image 28
Eilon Avatar answered Dec 15 '22 21:12

Eilon