Aar*_*ieb 35 asp.net-mvc html5
HTML5允许使用前缀为短语"data-"的自定义属性,该短语通过验证而不使用自定义DTD(更多信息).在Asp.Net MVC中,有没有办法用数据属性指定ActionLink?
向ActionLink添加属性的典型方法是传入一个匿名对象,每个对象都有一个自定义属性:
new { customattribute="value" }
Run Code Online (Sandbox Code Playgroud)
我想做的是:
new { data-customattribute="value" }
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为连字符在属性名称中无效.这种限制有什么办法吗?或者我只需要在使用ActionLinks和使用数据属性之间做出选择?
Nad*_*edr 74
或者你可以使用
new { data_customattribute="value" }
并且编译器足够聪明,可以知道你的意思
Çağ*_*kin 22
是的,方法有一个重载,ActionLink它IDictionary<string,object>取代了一个匿名对象.
<%=Html.ActionLink("text", "Index", "Home", null /*routeValues*/,
new Dictionary<string, object> {
{ "data-customattribute", "value" },
{ "data-another", "another-value" }
})%>
Run Code Online (Sandbox Code Playgroud)
产出:
<a data-another="another-value" data-customattribute="value" href="/">text</a>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14722 次 |
| 最近记录: |