How to pass routeValues that contains hyphen via actionlink in asp.net mvc 5
Asked Answered
A

3

7

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>
Adalbert answered 17/12, 2014 at 17:52 Comment(0)
S
3

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.

Sidonie answered 17/12, 2014 at 18:43 Comment(0)
I
6

If you are sure the url won't change much from the structure you've specified you could use an anchor tag and build the url from Url.Action.

<a href='@Url.Action("abc", "abc")?sap-ie=Edge'>abc</a>

The whole point of htmlhelpers are to help generate html anyway .. so if its getting in the way, you can drop down to html proper and just get it done.

Insensate answered 17/12, 2014 at 17:54 Comment(3)
I tried that. but is generates "sap_ie", not "sap-ie"Adalbert
@user3273146 i've modified my answer... would this work?Insensate
Yep. that's working. Just hope SAP doesn't plan on changing that in future. Thanks!Adalbert
C
3

Have you tried using this overload of the ActionLink method?

    @{
        var routeValues = new RouteValueDictionary();
        routeValues.Add("sap-ie", "Edge");
        routeValues.Add("area", "");

        var attributes = new Dictionary<string, object>();
        attributes.Add("Id", "nav_abc");
    }
    @Html.ActionLink("Go to contact page", "Contact", routeValues, attributes)
Corenda answered 17/12, 2014 at 18:2 Comment(2)
I tried both your approach & scartag's Both works. Yours is missing area and id should come as htmlattribute instead of routevalues. Thanks anywayAdalbert
Whoops, looks like I overlooked that. I updated my answer according to your feedback. Thanks!Corenda
S
3

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.

Sidonie answered 17/12, 2014 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.