【发布时间】:2014-01-17 14:42:56
【问题描述】:
我正在尝试创建 Html.ActionLink(...) HtmlHelper 的简单自定义版本
我想为传入的 htmlAttributes 匿名对象附加一组额外的属性。
public static MvcHtmlString NoFollowActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
var customAttributes = new RouteValueDictionary(htmlAttributes) {{"rel", "nofollow"}};
var link = htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, customAttributes);
return link;
}
所以在我看来我会这样:
@Html.NoFollowActionLink("Link Text", "MyAction", "MyController")
我希望呈现如下链接:
<a href="/MyController/MyAction" rel="nofollow">Link Text</a>
但是我得到了:
<a href="/MyController/MyAction" values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" count="1">Link Text</a>
我尝试了各种将匿名类型转换为 RouteValueDictionary 的方法,将其添加到根 ActionLink(...) 方法或转换为 Dictionary,或使用 HtmlHelper.AnonymousObjectToHtmlAttributes 并做同样的事情,但似乎没有任何效果。
【问题讨论】:
标签: c# asp.net .net asp.net-mvc-4