asp.net-mvc ActionLink htmlAttributes
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4108943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActionLink htmlAttributes
提问by Pavel Hlobil
WORKS
作品
<a href="@Url.Action("edit", "markets", new { id = 1 })"
data-rel="dialog" data-transition="pop" data-icon="gear" class="ui-btn-right">Edit</a>
DOES NOT WORK - WHY?
不起作用 - 为什么?
@Html.ActionLink("Edit", "edit", "markets", new { id = 1 }, new {@class="ui-btn-right", data-icon="gear"})
It seems you can't pass something like data-icon="gear" into htmlAttributes?
似乎您无法将 data-icon="gear" 之类的内容传递给 htmlAttributes?
Suggestions?
建议?
回答by marcind
The problem is that your anonymous object property data-icon
has an invalid name. C# properties cannot have dashes in their names. There are two ways you can get around that:
问题是您的匿名对象属性data-icon
的名称无效。C# 属性的名称中不能有破折号。有两种方法可以解决这个问题:
Use an underscore instead of dash (MVC will automatically replace the underscore with a dash in the emitted HTML):
使用下划线而不是破折号(MVC 将自动用发出的 HTML 中的破折号替换下划线):
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new {@class="ui-btn-right", data_icon="gear"})
Use the overload that takes in a dictionary:
使用接受字典的重载:
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new Dictionary<string, object> { { "class", "ui-btn-right" }, { "data-icon", "gear" } });
回答by hemp
Replace the desired hyphen with an underscore; it will automatically be rendered as a hyphen:
用下划线替换所需的连字符;它将自动呈现为连字符:
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new {@class="ui-btn-right", data_icon="gear"})
becomes:
变成:
<form action="markets/Edit/1" class="ui-btn-right" data-icon="gear" .../>
回答by amirhossein fallahmanesh
@Html.ActionLink("display name", "action", "Contorller"
new { id = 1 },Html Attribute=new {Attribute1="value"})