我正在使用 MVC 5,并且正在尝试编写一些 Bootstrap 扩展方法。我的目标是“覆盖”Html.ActionLink
方法与Html.BootstrapLinkButton
. The BootstrapLinkButton
方法应该生成与 css 类的链接"btn btn-default"
自动附加。
到目前为止我的代码:
public static MvcHtmlString BootstrapLinkButton(this HtmlHelper htmlHelper,
string linkText,string actionName, string controllerName,
object routeValues = null, object htmlAttributes = null)
{
var attributes =
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (attributes.ContainsKey("class"))
{
object value;
attributes.TryGetValue("class", out value);
value = (value as string) + " btn btn-default";
attributes["class"] = value;
}
else
{
attributes["class"] = "btn btn-default";
}
return htmlHelper.ActionLink(
linkText, actionName, controllerName, routeValues,
new Dictionary<string, object>(attributes));
}
这在 HTML 中给出了以下结果:
<a comparer="System.Collections.Generic.GenericEqualityComparer`1[System.String]"
count="3"
keys="System.Collections.Generic.Dictionary`2
+KeyCollection[System.String,System.Object]"
values="System.Collections.Generic.Dictionary`2
+ValueCollection[System.String,System.Object]"
href="/test/test/">
Test
</a>
我在互联网上搜索,但似乎没有什么可以解决这个问题。有谁知道解决这个问题的神奇代码?