tags:

views:

37

answers:

2

since my app has a sidebar menu that gets built dynamically through an xml everytime I want to load a view I need to pass all of this parameters.

        ViewData["mainItems"] = navigation.getItems();
        ViewData["controller"] = controller;//this is just a string with the name of my  controller

how can I avoid doing this everytime but that the parameters keep getting being retrieved and passed?

A: 

If i understand what your problem is correctly, then you might want to create a base class controller and make all your controller inherit from this base controller. Then override the OnActionExecuted event which gets fired on every action.

public class BaseController : Controller {
   protected override void OnActionExecuted(ActionExecutedContext filterContext) {
      ViewData["mainItems"] = navigation.getItems();
      ViewData["controller"] = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
      base.OnActionExecuted(filterContext);
}
Jonas Stawski
+1  A: 

You don't really need to pass the controller name. you have it in the viewContext.

See this SO question : Find what ASP.NET MVC controller in master view

CD