ASP.NET MVC/C#:我可以使用 Html.ActionLink() 创建有效的自定义 HTML 属性吗?
我需要在使用 Html.ActionLink() 构建的锚点上放置一个自定义属性,
<%: Html.ActionLink("Delete", "Delete", new { id = Model.ID }, new { data-icon = "ui-icon-trash" })%>
使用正确的“data-”前缀,按照 http://www.w3.org/TR/html5/elements.html#attr-data,我从 Visual Studio 收到以下错误。
无效的匿名类型成员声明符。匿名类型成员必须使用成员赋值、简单名称或成员访问权限来声明。
由于我无法在匿名类型中使用连字符,那么添加自定义 HTML 属性的最佳方法是什么?
I have the need to put a custom attribute on an anchor which I am constructing using Html.ActionLink()
<%: Html.ActionLink("Delete", "Delete", new { id = Model.ID }, new { data-icon = "ui-icon-trash" })%>
Using the proper "data-" prefix, as per http://www.w3.org/TR/html5/elements.html#attr-data, I get the following error from Visual Studio.
Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
Since I can't use a hyphen in the anonymous type, what would be the best way to go about adding my custom HTML attribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
data-icon
不是有效的 C# 变量名称。您可以获得的最接近的是:当然这个问题已在 中解决ASP.NET MVC 3,您不再需要编写意大利面条式代码。所以:
下划线将自动转换为连字符。
data-icon
is not a valid C# variable name. The closest you could get is this:Of course this issue has been addressed in ASP.NET MVC 3 and you no longer need to write spaghetti code. So:
And the underscore will be automatically converted to a hyphen.