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

后端 未结 3 649
刺人心
刺人心 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 06:47

    You don't need to pass route data to a controller because the controller already has knowledge of it via the RouteData property:

    public ActionResult Index() {
        // You could use this.RouteData here
        ...
    }
    

    Now if you want to pass some simple arguments like current action that was used to render the view you could do this:

    <%= Html.RenderAction(
        "MenuOfStreamEntries",
        "Nav",
        new {
            currentStreamUrl = "Blog", 
            currentAction = ViewContext.RouteData.Values["action"],
            currentController = ViewContext.RouteData.Values["controller"]
        }
    ); %>
    

    And in your controller:

    public ActionResult Nav(string currentAction, string currentController) 
    {
        ...
    }
    

提交回复
热议问题