ASP.NET MVC / C#:我可以创建一个使用Html.ActionLink有效的自定义HTML属性()? [英] ASP.NET MVC/C#: Can I create valid custom HTML attributes using Html.ActionLink()?
问题描述
我有必要把一个自定义属性上我使用的Html.ActionLink构建一个锚()
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" })%>
使用适当的数据 - preFIX,按<一个href=\"http://www.w3.org/TR/html5/elements.html#attr-data\">http://www.w3.org/TR/html5/elements.html#attr-data,我从Visual Studio碰到下面的错误。
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.
既然不能在匿名类型使用连字符,这将是去有关添加我的自定义HTML属性的最佳方式?
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?
推荐答案
数据图标
不是一个有效的C#变量名。你可以得到最接近的是这样的:
data-icon
is not a valid C# variable name. The closest you could get is this:
<%: Html.ActionLink(
"Delete",
"Delete",
new { id = Model.ID },
new Dictionary<string, string> { { "data-icon", "ui-icon-trash" } }
) %>
当然,这个问题一直在 ASP.NET MVC 3 解决,你不再需要编写意大利面条code。所以:
Of course this issue has been addressed in ASP.NET MVC 3 and you no longer need to write spaghetti code. So:
<%: Html.ActionLink(
"Delete",
"Delete",
new { id = Model.ID },
new { data_icon, "ui-icon-trash" }
) %>
以及下划线的将被自动转换为的连字符的
这篇关于ASP.NET MVC / C#:我可以创建一个使用Html.ActionLink有效的自定义HTML属性()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!