c0shea/WebHelpers.Mvc5

Add IsTreeViewActive

Closed this issue · 2 comments

Need a way to set the active class on both the treeview and its child element.

Maybe use something like this?

        /// <summary>
        /// Gets the CSS class to use for the treeview state. If the current request route matches
        /// any of the <paramref name="actions"/>, the "active" class is returned.
        /// </summary>
        /// <param name="url">The <see cref="UrlHelper"/>.</param>
        /// <param name="actions">A collection of KeyValuePairs, where the key is the action and the value is the controller.</param>
        /// <returns></returns>
        public static string IsTreeviewActive(this UrlHelper url, Dictionary<string, string> actions)
        {
            var controller = url.RequestContext.RouteData.Values["controller"].ToString();
            var action = url.RequestContext.RouteData.Values["action"].ToString();

            if (actions.Any(a => a.Key == action && a.Value == controller))
            {
                return "active";
            }

            return "";
        }

I have rewrite as
public static string IsTreeviewActive2(this UrlHelper url, List<KeyValuePair<string, string>> actions)
{
var controller = url.RequestContext.RouteData.Values["controller"].ToString();
var action = url.RequestContext.RouteData.Values["action"].ToString();

        if (actions.Any(a => a.Key == action && a.Value == controller))
        {
            return "active";
        }

        return "";
    }

var nodes2 = new List<KeyValuePair<string, string>>();
nodes2.Add(new KeyValuePair<string, string>("Index", "HRApplier"));
nodes2.Add(new KeyValuePair<string, string>("Index", "Applier"));

cause Dictionary<string, string> not work for many controllers with the same action name.