I'm using MVC 5 and I'm trying to write some Bootstrap extention methods. My goal is to 'overwrite' the Html.ActionLink
method with Html.BootstrapLinkButton
. The BootstrapLinkButton
method should generate a link with the css classes "btn btn-default"
automatically attached.
My code so far:
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));
}
which gives me the following result in the 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>
I searched the internet, but nothing seems to fix this problem. Does anyone knows the magic code to fix this?
It's already implemented in TwitterBootstrapMVC
Here is some relevant code
Most likely the reason your code is failing is because
htmlHelper.ActionLink(...)
method there is confused about which overload needs to be used. There is no overload on it that takesstring, string, object, Dictionary
, which is what you are trying to pass to it.If my solution could help anyone, here it is: