9

I hope this is just a bug but figured maybe it was just me.

@Html.ActionLink("Test", "Test", "Test",
  new { id = 1 },
  new Dictionary<string, object> { { "class", "ui-btn-test" }, { "data-icon", "gear" } })

This does work but if I wanted to add further attributes I have to do it manually!

@Html.ActionLink("Test", "Test", "Test",
  new { id = 1 },
  new { @class="ui-btn-test", data_icon="gear", data_new_attr="someextra" })

The first doesn't work anymore and I need this one to work. The second works but don't care that it does, because I'm trying to add more attributes, object will not work unless told differently.

4
  • What's the question? The second way is the preferred way to specify htmlAttributes. Commented May 9, 2012 at 1:48
  • In MVC 4 the first my preferred way doesn't work correctly. I get something like this as the attributes. <a Comparer="System.Collections.Generic.GenericEqualityComparer1[System.String]" Count="1" Keys="System.Collections.Generic.Dictionary2+KeyCollection[System.String,System.Object]" Values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" href="/Home">Home</a> Commented May 9, 2012 at 16:13
  • Why would you want to do it the first way. The second way works, is accepted practice, and is easier to read. Commented May 9, 2012 at 21:26
  • 2
    I thought, I explained myself. The reason is for the ability to add more attributes at runtime. Beside's it is a method in which is depicted in the documentation as an acceptable parameter, so why am I having trouble with using the method described? Commented May 10, 2012 at 15:42

1 Answer 1

10

None of the provided ActionLink helper methods accept parameters like you're specifying. The only ones that take a parameter of type IDictionary for the html attributes expect a RouteValueDictionary for the route values. As it is, you're currently calling a function that is treating your dictionary as an anonymous object

Try

@Html.ActionLink("Test", "Tesz", "Test",
new RouteValueDictionary(new {id = 1}),
new Dictionary<string, object> { { "class", "ui-btn-test" }, { "data-icon", "gear" } })

Or implement your own extension method.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked out great! I was about to implement my own extension method, but didn't want to rewrite all my action links.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.