tags:

views:

89

answers:

4

Hi,

I have set up a menu-controller to drive the top menu links based on which other controller is being used. Each other controller has a separate nested master page for each of its views.

so, i have a menu-controller with several methods that return viewresults, one per each controller, or "section" of the site. So currently each of these methods has its own view to render the menu. but each view to render the menu is the same code, the only thing that changes is the logic in the controller methods based on which links to render.

is there any way to have all of these controller actions target the same view? since the view is the same for all?

thanks

+3  A: 

Yes, that is a common practice.

return View("Menu");
F.Aquino
A: 

Create a strongly typed view that takes a container specifying your menu content. Pass this as a parameter on your return statement.

 var thisMenu = CreateMenuForThisRequest();
 return View ("Menu", thisMenu);
No Refunds No Returns
+1  A: 

it depends on what version of ASP MVC you're using; with MVC 2, you can create an ascx control and use RenderAction

in your view you'll put something like

Html.RenderAction("Menu", "Navigation");

and have a navigation controller with a Menu actionresult


public class NavigationController : Controller
{
      [ChildActionOnly]
      public ActionResult Menu()
      {
        Menu model;//your menu
        return PartialView("YourMenuAscxControlName", model);
      }
}

I think if you're using MVC 1, the MVC Future project has the RenderAction but i'm not sure.

Emmanuel
MVC Future for MVC 1 has the RenderAction
freddoo
A: 

For my menu I use the RenderAction method I'm also using the ActionOutputCacheAttribute from Steve Sanderson http://blog.stevensanderson.com/2008/10/15/partial-output-caching-in-aspnet-mvc/

you will greatly increase your site loading time with this caching

freddoo