In ASP.NET MVC 2 - How do I get Route Values into my navigation controller so I can highlight the current link?

后端 未结 3 645
刺人心
刺人心 2021-01-06 06:26

I\'m trying to get the current Route into my navigation controller so I can run comparisons as the navigation menu data is populated.

My Links object is like this:

3条回答
  •  旧时难觅i
    2021-01-06 06:58

    Here's a sample method we wrote for doing something similar.

    public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)
    {
      string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
      string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
    
      return currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase));
    }
    

提交回复
热议问题