无法使用将 html 代码包装在锚标记内@Html.ActionLink
您可以使用@Url.Action
<a href="@Url.Action("UserManagment", "Admin")" ...>
<i class="icon-user"></i>User Managment
</a>
或者创建一个扩展方法:
public static IHtmlString MyActionLink(this HtmlHelper helper,
string value,
Dictionary<string, string> attributes,
string innerHtml)
{
var aBuilder = new TagBuilder("a");
foreach (var attr in attributes)
{
aBuilder.MergeAttribute(attr.Key, attr.Value);
}
aBuilder.InnerHtml += innerHtml + value;
return new HtmlString(aBuilder.ToString(TagRenderMode.Normal));
}
并像这样在您的视图中使用它
@{
var attributes = new Dictionary<string, string>
{
{ "data-toggle", "collapse" },
{ "data-parent", "#leftMenu" },
{ "href", Url.Action("UserManagement", "Admin") }
};
var innerHtml = "<i class='icon-user'></i>";
}
@Html.MyActionLink("User Managment", attributes, innerHtml)