MVC 4 ActionLink Dictionary htmlAttributes 不起作用



我希望这只是一个错误,但我想也许只是我。

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

这确实有效,但如果我想添加更多属性,我必须手动完成!

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

第一个不再起作用,我需要这个来工作。第二个有效但不在乎它是否有效,因为我正在尝试添加更多属性,除非以不同的方式告诉,否则对象将不起作用。

提供的ActionLink帮助程序方法均不接受您指定的参数。只有那些为 html 属性采用 IDictionary 类型的参数的参数需要路由值的 RouteValueDictionary。实际上,您当前正在调用一个将字典视为匿名对象的函数

尝试

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

或者实现自己的扩展方法。

最新更新