Just wanted to point out that it's not that the underscore trick only works with data
attributes, it's that it only works with passing HTML attributes in general. This is because it makes sense to change underscores to hyphens in the context of HTML, as underscores aren't use in HTML attributes. However, it's perfectly valid for you to have a route param with an underscore, so the framework can make no assumptions about your intent.
If you need to pass route values with hyphens, you have to use a RouteValueDictionary
. This is simply a limitation of anonymous objects that can't be overcome.
<li>@Html.ActionLink("abc", "abc", "abc", new RouteValueDictionary { { "area", "" }, "sap-ie", "Edge" } }, new RouteValueDictionary { { "id", "nav_abc" } })</li>
Unfortunately, there's no ActionLink
overload that accepts both a RouteValueDictionary
for routeValues
and an anonymous object for htmlAttributes
, so switching one means switching both. You can technically use any IDictionary
implementation for the htmlAttributes
param, so you may prefer to use just new Dictionary { { "id", "nav_abc" } }
. It's up to you.