1

I written a custom HtmlHelper as the following:

public static MvcHtmlString MdActionLink(this HtmlHelper htmlHelper, string resourceId, string actionName, string controllerName, object routeValues = null, object htmlAttributes = null)
{
    if (routeValues == null && htmlAttributes != null)
        return htmlHelper.ActionLink(ResourcesHelper.GetMessageFromResource(resourceId), actionName, controllerName, htmlAttributes);

    return htmlHelper.ActionLink(ResourcesHelper.GetMessageFromResource(resourceId),
        actionName, controllerName,
        routeValues,
        htmlAttributes);
}

It's OK if routeValues and htmlAttributes both were null.
But if htmlAttributes has a value and routeValues was null, it render a tag as the follows:

<a comparer="System.Collections.Generic.GenericEqualityComparer`1[System.String]" count="1" keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" href="/Home/Login?Length=7">Exit</a>

What's wrong with it?

Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232
  • I've just tested it, it doesn't change. – Mohammad Dayyan May 09 '15 at 12:51
  • 2
    The same as usual, I guess, wrong overload http://stackoverflow.com/questions/4357856/razor-actionlink-autogenerating-length-7-in-url – Wiktor Zychla May 09 '15 at 12:56
  • The only overload of `ActionLink` that accepts a `string` as the first 3 parameters also requires the 4th parameter as the route values and the 5th parameter as the html attributes, meaning if your `if` block evaluates to true, your rending the htmlAttributes as the route values. Just delete this helper - it does nothing at all that the inbuilt `ActionLink()` helpers don't already do. –  May 10 '15 at 03:11

1 Answers1

1

Try this:

public static MvcHtmlString MdActionLink(this HtmlHelper htmlHelper, string resourceId, string actionName, string controllerName, object routeValues = null, object htmlAttributes = null)
{
    if (routeValues == null)
         routeValues = new RouteValueDictionary();

     if (htmlAttributes == null)
          htmlAttributes = new Dictionary<string, object>();

     htmlHelper.ActionLink(ResourcesHelper.GetMessageFromResource(resourceId),
        actionName, controllerName,
        routeValues,
        htmlAttributes);
}
Itay
  • 16,601
  • 2
  • 51
  • 72