开发者

ASP.NET MVC2 - How to call another controller with RenderAction() as well as pass arguments to that controller?

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 d开发者_如何学Goefined? 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.


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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜