Razor MVC4ActionLink字典htmlAttributes不';行不通

Razor MVC4ActionLink字典htmlAttributes不';行不通,razor,asp.net-mvc-4,Razor,Asp.net Mvc 4,我希望这只是一个bug,但我想可能只是我 @Html.ActionLink("Test", "Test", "Test", new { id = 1 }, new Dictionary<string, object> { { "class", "ui-btn-test" }, { "data-icon", "gear" } }) 第一个不起作用了,我需要这个来起作用。第二种方法可以工作,但不必在意,因为我正在尝试添加更多属性,除非有不同的说明,否则对象将无法工作。提供的Ac

我希望这只是一个bug,但我想可能只是我

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

第一个不起作用了,我需要这个来起作用。第二种方法可以工作,但不必在意,因为我正在尝试添加更多属性,除非有不同的说明,否则对象将无法工作。

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

试一试

@Html.ActionLink(“测试”、“测试”、“测试”,
新的RouteValueDictionary(新的{id=1}),
新字典{{“类”、“ui btn测试”}、{“数据图标”、“齿轮”})

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

问题是什么?第二种方法是指定
htmlAttributes
的首选方法。在MVC4中,我首选的第一种方法不能正常工作。我得到这样的属性。你为什么要用第一种方法。第二种方法有效,是公认的做法,而且更容易阅读。原因是能够在运行时添加更多属性。除此之外,它是一种方法,在文档中被描述为一个可接受的参数,那么为什么我在使用所描述的方法时会遇到问题呢?这很好!我正要实现我自己的扩展方法,但不想重写我所有的操作链接。
@Html.ActionLink("Test", "Test", "Test",
  new { id = 1 },
  new { @class="ui-btn-test", data_icon="gear", data_new_attr="someextra" })
@Html.ActionLink("Test", "Tesz", "Test",
new RouteValueDictionary(new {id = 1}),
new Dictionary<string, object> { { "class", "ui-btn-test" }, { "data-icon", "gear" } })