I am writing a helper for my application which writes out a menu item for a given strongly typed controller/action as follows:
<%= Html.MenuLink<WhateverController>(c => c.WhateverAction(), "Whatever") %>
As part of this process, I would like to apply the class of active
to the outputted link if the current page and the page linked to are the same. I figure the best way of doing it is to compare the contents of the RouteValueDictionary
for the current request to that of the result of the Expression given to the helper method. However, I cannot figure out a good way of comparing whether the items in the two RouteValueDictionary
s are the same.
Is there a simple way of doing this? I effectively want to complete it in the following way:
public static string MenuLink<T>(this HtmlHelper html, Expression<Action<T>> action, string linkText) where T : Controller
{
// var link = html.ActionLink<T>(action, linkText, new {}); // Not important yet
var routeValues = Microsoft.Web.Mvc.Internal.ExpressionHelper.GetRouteValuesFromExpression<T>(action); // Might change?
var currentRouteVals = html.ViewContext.RouteData.Values;
bool isActivePage = /* are the contents of routeValues also
inside currentRouteValues? */
var tb = new TagBuilder("li");
// Continues...
}
I have tried using the built-in comparison (==), but it seems that it is using the default equality implementation and therefore returns false since they are not the same instance. I have also tried the following:
bool isActivePage = routeValues.All(x => currentRouteVals.ContainsValue(x));
but that doesn't work either. Am I completely barking up the wrong tree?