MVC 4 ActionLink Dictionary htmlAttributes doesn't work

主宰稳场 提交于 2019-12-22 05:07:34

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/10507737/mvc-4-actionlink-dictionary-htmlattributes-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!