ActionLink htmlattributes
-
29-09-2019 - |
题
作品
<a href="@Url.Action("edit", "markets", new { id = 1 })"
data-rel="dialog" data-transition="pop" data-icon="gear" class="ui-btn-right">Edit</a>
不起作用 - 为什么?
@Html.ActionLink("Edit", "edit", "markets", new { id = 1 }, new {@class="ui-btn-right", data-icon="gear"})
看来您不能将诸如data-icon =“ gear”之类的东西传递到htmlattributes中?
建议?
解决方案
问题是您的匿名对象属性 data-icon
有一个无效的名称。 c#属性不能以其名称中的破折号。您可以通过两种方法来解决:
使用下划线而不是破折号(MVC将自动用发射的HTML中的破折号替换下划线):
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new {@class="ui-btn-right", data_icon="gear"})
使用词典包含的过载:
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new Dictionary<string, object> { { "class", "ui-btn-right" }, { "data-icon", "gear" } });
其他提示
用下划线代替所需的连字符;它将自动呈现为连字符:
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new {@class="ui-btn-right", data_icon="gear"})
变成:
<form action="markets/Edit/1" class="ui-btn-right" data-icon="gear" .../>
@Html.ActionLink("display name", "action", "Contorller"
new { id = 1 },Html Attribute=new {Attribute1="value"})
不隶属于 StackOverflow