开发者

Html.ActionLink with non-C# friendly htmlAttribute names

开发者 https://www.devze.com 2023-01-24 06:22 出处:网络
Disqus wants me to add an attribute called data-disqus-identifier to my links, but for obvious reasons, n开发者_StackOverflow社区ew { @data-disqus-identifier = \"article\" } gives me a syntax error.

Disqus wants me to add an attribute called data-disqus-identifier to my links, but for obvious reasons, n开发者_StackOverflow社区ew { @data-disqus-identifier = "article" } gives me a syntax error.

Any idea what to do in these situations?

Thanks,

Rei


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.


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" } 
0

精彩评论

暂无评论...
验证码 换一张
取 消