Dave Mateer
Dave Mateer

Reputation: 17946

Append to routeValues in HtmlHelper extension method

I want to create a simple extension of HtmlHelper.ActionLink that adds a value to the route value dictionary. The parameters would be identical to HtmlHelper.ActionLink, i.e.:

public static MvcHtmlString FooableActionLink(
    this HtmlHelper html,
    string linkText,
    string actionName,
    string controllerName,
    object routeValues,
    object htmlAttributes)
{
  // Add a value to routeValues (based on Session, current Request Url, etc.)
  // object newRouteValues = AddStuffTo(routeValues);

  // Call the default implementation.
  return html.ActionLink(
      linkText, 
      actionName, 
      controllerName, 
      newRouteValues, 
      htmlAttributes);
}

The logic for what I am adding to routeValues is somewhat verbose, hence my desire to put it in an extension method helper instead of repeating it in each view.

I have a solution that seems to be working (posted as an answer below), but:

Please post any suggestions for improvement or better solutions.

Upvotes: 7

Views: 4444

Answers (1)

Dave Mateer
Dave Mateer

Reputation: 17946

public static MvcHtmlString FooableActionLink(
    this HtmlHelper html,
    string linkText,
    string actionName,
    string controllerName,
    object routeValues,
    object htmlAttributes)
{
    // Convert the routeValues to something we can modify.
    var routeValuesLocal =
        routeValues as IDictionary<string, object>
        ?? new RouteValueDictionary(routeValues);

    // Convert the htmlAttributes to IDictionary<string, object>
    // so we can get the correct ActionLink overload.
    IDictionary<string, object> htmlAttributesLocal =
        htmlAttributes as IDictionary<string, object>
        ?? new RouteValueDictionary(htmlAttributes);

    // Add our values.
    routeValuesLocal.Add("foo", "bar");

    // Call the correct ActionLink overload so it converts the
    // routeValues and htmlAttributes correctly and doesn't 
    // simply treat them as System.Object.
    return html.ActionLink(
        linkText,
        actionName,
        controllerName,
        new RouteValueDictionary(routeValuesLocal),
        htmlAttributesLocal);
}

Upvotes: 13

Related Questions