Rei Miyasaka
Rei Miyasaka

Reputation: 7096

Html.ActionLink with non-C# friendly htmlAttribute names

Disqus wants me to add an attribute called data-disqus-identifier to my links, but for obvious reasons, new { @data-disqus-identifier = "article" } gives me a syntax error.

Any idea what to do in these situations?

Thanks,

Rei

Upvotes: 0

Views: 601

Answers (2)

marcind
marcind

Reputation: 53183

In MVC 3, if you use underscore _ instead of hyphens - in your property names then MVC will automatically convert them to hyphens in the resulting HTML. So, something like this should work for you:

new { data_disqus_identifier = "article" } 

Upvotes: 0

SLaks
SLaks

Reputation: 887225

You can pass a Dictionary<string, object> with arbitrary string keys.
The syntax will be more verbose: new Dictionary<string, object> { { "data-disqus-identifier", "article" } }.

You may want to create an extension method or a static method on a static class with a short name that takes a smaller parameter set and returns the dictionary.

Upvotes: 4

Related Questions