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

后端 未结 3 640
刺人心
刺人心 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条回答
  •  [愿得一人]
    2021-01-06 07:04

    Taken from this good article.

    Create a custom extension method:

    public static class HtmlExtensions
    {
        public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper, String linkText,
            String actionName, String controllerName)
        {
            var tag = new TagBuilder("li");
    
            if ( htmlHelper.ViewContext.RequestContext
                .IsCurrentRoute(null, controllerName, actionName) )
            {
                tag.AddCssClass("selected");
            }
    
            tag.InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToString();
    
            return MvcHtmlString.Create(tag.ToString());
        }
    }
    

    Your HTML markup:

    
    

    Produces the following output:

    
    

提交回复
热议问题