加入htmlAttributes定制ActionLink的帮手扩展帮手、htmlAttributes、ActionLink

2023-09-03 16:07:09 作者:错梦江南

我试图创建Html.ActionLink(...)的HtmlHelper

I'm trying to create a simple custom version of the Html.ActionLink(...) HtmlHelper

我要追加一组的附加属性为传入的htmlAttributes annonymous对象。

I want to append a set of extra attributes to the htmlAttributes annonymous object passed in.

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;
}

所以,我认为我有这样的:

So in my view I would have this:

@Html.NoFollowActionLink("Link Text", "MyAction", "MyController")

这我希望呈现出如同链接:

Which I'd expect to render out a link like:

<a href="/MyController/MyAction" rel="nofollow">Link Text</a>

而是我得到:

But instead I get:

<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>

我已经试过转换annonymous型成RouteValueDictionary的各种方法,增加了它,然后通过在根ActionLink的(...)方法或转换为字典,或者使用HtmlHelper.AnonymousObjectToHtmlAttributes和做相同的,但似乎没有任何工作。

I've tried various methods of converting the annonymous type into a RouteValueDictionary, adding to it then passing that in to the root ActionLink(...) method OR converting to Dictionary, OR using HtmlHelper.AnonymousObjectToHtmlAttributes and doing the same but nothing seems to work.

推荐答案

你得到的结果是造成该源$c$c

The result you get is caused by this source code :

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
    return ActionLink(htmlHelper, linkText, actionName, controllerName, TypeHelper.ObjectToDictionary(routeValues), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}

正如你可以看到 HtmlHelper.AnonymousObjectToHtmlAttributes 被内部调用。这就是为什么你得到当你通过它 RouteValueDictionary 对象。

As you can see HtmlHelper.AnonymousObjectToHtmlAttributes is called inside. That's why you get values and keys when you pass it RouteValueDictionary object.

有只有两种方法相匹配的参数列表:

There are only two methods matching your argument list:

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)

第二个重载不会做任何事情,为您的参数,只传递他们。 您需要修改code调用其他的过载:

The second overload doesn't do anything to your parameters, just passes them. You need to modify your code to call the other overload:

public static MvcHtmlString NoFollowActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues = null, object htmlAttributes = null)
{
    var routeValuesDict = new RouteValueDictionary(routeValues);

    var customAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    if (!customAttributes.ContainsKey("rel"))
        customAttributes.Add("rel", "nofollow");

    return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValuesDict, customAttributes);
}

当你通过 routeValues​​ RouteValueDictionary 其他过载回升( RouteValueDictionary 正在实施的IDictionary&LT;字符串,对象&gt; 所以它的罚款),返回的链接是正确的。

When you pass routeValues as RouteValueDictionary the other overload is picked (RouteValueDictionary is implementing IDictionary<string, object> so it's fine), and the returned link is correct.

如果在相对属性将已经美元的 htmlAttributes 目标P $ psent,一个异常将被抛出:

If the rel attribute will be already present in htmlAttributes object, an exception will be thrown:

System.ArgumentException: An item with the same key has already been added.
 
精彩推荐
图片推荐