views:

67

answers:

1

I'm trying to render a partial view on my master page using Html.RenderAction() in VS 2010 using MVC2. Here is my RenderAction() call:

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

However, when it its the Navigation controller's constructor, it always hits the constructor that is defined with no arguments.

public class NavigationController : Controller
{
    public NavigationViewModel navigationViewModel { get; set; }

    public NavigationController()
    {
        -snip-
    }

    public NavigationController( string currentAction, string currentController )
    {
        -snip-
    }

    [ChildActionOnly]
    public ViewResult Menu()
    {
        return View(this.navigationViewModel);
    }
}

Of all the examples I have seen, this is how you would pass arguments using a RenderAction() call. I don't get any error messages other than it complains if I remove the constructor with no arguments defined.

How can I get it to call the constructor which has the two arguments defined? I'd like to be able to compare to the currentAction and currentController when building the menu to correctly highlight the section the user is currently on.

+5  A: 

According to your example you're passing the arguments through to the action, not the controller constructor.

So actually, what I think you should be doing is more like this

public class NavigationController
{
    [ChildActionOnly]
    public ViewResult Menu(string currentAction, string currentController)
    {
        var navigationViewModel = new NavigationViewModel();

        // delegates the actual highlighing to your view model
        navigationViewModel.Highlight(currentAction, currentController);

        return View(navigationViewModel);
    }
}
Dave
Ahah! You are correct, thank you sir!
Sgraffite