Question

I have created a binding to create a tag for Html.ActionLink html helper like this that works fine:

<element name="actionlink">
    Html.ActionLink("child::*", "@action", new RouteValueDictionary{{"@route-*"}})
</element>

Using the tage like <actionlink action="index" route-controller="users" route-id="${Model.Id}"/> creates the anchor tag correctly with the correct href attribute as I would expect.

However, I have then tried overloading the binding on this tag so I can add html attributes such as id, class etc, like this:

<element name="actionlink">
    Html.ActionLink("child::*", "@action", new RouteValueDictionary{{"@route-*"}}, new Dictionary[[string,object]]{{"@*"}})
</element>

You will notice it has the wild card being passed to the last Dictionary parameter, which is the html attributes parameter of the HtmlHelper. However, when I use the actionlink tag in the following way:

<actionlink action="index" route-controller="users" route-id="${Model.Id}" id="linkId" class="className"/>

It creates the same markup as the usage above, i.e. it doesn't include the id and class html attributes. Am I doing something fundamentally wrong here? Can anybody see what is incorrect?

Was it helpful?

Solution

When using bindings the thing you have to be aware of is that bindings are matched in the order they are declared so if you have the bindings declared in the order that you have specified above, the second binding would never get matched.

If you switch the bindings to the order below, then it should work:

<!-- most specific binding first -->
<element name="actionlink">
    Html.ActionLink("child::*", "@action", new RouteValueDictionary{{"@route-*"}}, new Dictionary[[string,object]]{{"@*"}})
</element>
<element name="actionlink">
    Html.ActionLink("child::*", "@action", new RouteValueDictionary{{"@route-*"}})
</element>

Also, when using bindings you need to be aware that changes don't get picked up until you rebuild your solution, so if you're making changes, they could be right, but it might just not look like they're getting picked up because you haven't rebuilt yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top