I have an actionlink in view, I need it to pass parameter containing hyphen (-). Changing the name convention is not possible. How do I do this?
<li>@Html.ActionLink("abc", "abc", "abc", new { area = "",sap-ie="Edge" }, new { id = nav_abc"})</li>
This one gives me an error "Invalid Anonymous Type Declarator" since it contains hyphen. Basically I need the actionlink to generate html like below.
<a href=".../abc?sap-ie=Edge" id="nav_abc" >abc</a>
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.Unfortunately, there's no
ActionLink
overload that accepts both aRouteValueDictionary
forrouteValues
and an anonymous object forhtmlAttributes
, so switching one means switching both. You can technically use anyIDictionary
implementation for thehtmlAttributes
param, so you may prefer to use justnew Dictionary { { "id", "nav_abc" } }
. It's up to you.