MVC 4 ActionLink的字典htmlAttributes不起作用 [英] MVC 4 ActionLink Dictionary htmlAttributes doesn't work

查看:131
本文介绍了MVC 4 ActionLink的字典htmlAttributes不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这只是一个bug,但也许想通这只是我的。

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.

推荐答案

所提供的 ActionLink的 helper方法接受无参数,如你指定。称取的IDictionary类型的参数为HTML属性的唯一期望的路线值RouteValueDictionary。正因为如此,你现在打电话是治疗你的字典作为一个匿名对象的函数

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

尝试

@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.

这篇关于MVC 4 ActionLink的字典htmlAttributes不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆