Asp – Context-sensitive ASP.NET MVC navigation menu using strongly typed views

asp.net-mvc

I am trying to figure out the best way to create a navigation menu in ASP.NET MVC that can change based on the controller action from which it was built (it would also be different based on user permissions and such). The navigation menu is displayed in the Master Page, and all of our views are strongly typed.

I wanted to use OnActionExecuting in a base controller to populate the standard menu, then modify it accordingly within each controller action. This didn't seem to be an option though since the view model wouldn't be available until my action is called.

The only other thing I could come up with was to pre-populate the menu object in the base ViewModel constructor. Then I could add/remove as necessary in my controller actions. This didn't seem entirely appropriate though since I'd be instantiating links back to controller actions in the ViewModel constructor (since each Menu item can have a controller/action/id).

Any suggestions on the best way to do this? Particularly for navigation menus (or possibly treeviews) that change dramatically based on context.

Best Answer

I use a MasterModel for this kind of thing.

Given that the .aspx and its .master are separable (e.g. you can swap out the master when rendering the aspx; further the aspx does not inherit from the master, it only uses the master) in my mind, the view models should therefore be separate as well. (i.e. PageModel should not inherit from MasterModel.)

The aspx might only care to receive, say, an IEnumerable<Foo>, why should it's view model be required to inherit from some common class?

So instead of:

public class MasterModel
{
    public string Title { get; set; }
    public MenuModel Menu { get; set; }
}

public class PageViewModel
    : MasterModel
{
    public IEnumerable<Foo> TheOnlyThingInTheWorldMyPageReallyCaresAbout { get; set; }
}

public ActionResult TheAction()
{
    var items = GetItems();
    var model = new PageViewModel {
        TheOnlyThingInTheWorldMyPageReallyCaresAbout = items
    };
    return view("Viewname", model);
}

I use:

public class BaseController
    : Controller
{
    protected MasterModel MasterModel
    {
        get { return ViewData["MasterModel"] as MasterModel; }
        set { ViewData["MasterModel"] = value; }
    }
}

public class MasterModel
{
    public MasterModel()
    {
        // Sensible defaults
    }
    
    public string Title { get; set; }
    public MenuModel Menu { get; set; }
}

public ActionResult TheAction()
{
    var items = GetItems();
    
    // Prepare the MasterModel as part of the controller's "select and prepare the view" responsibility.
    // Common cases can be factored into ActionFilter attributes or to a base Controller class.
    MasterModel = new MasterModel();
    return view("Viewname", items);
}

You can then add this to your master page:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<script runat="server">
    public MasterModel Model { get { return ViewData["MasterModel"] as MasterModel; } }
</script>

Now you don't have to do anything special to your page view models. They can care about only what they should be caring about.

Why is all this roundabout stuff interesting?

I wanted to use OnActionExecuting in a base controller to populate the standard menu, then modify it accordingly within each controller action. This didn't seem to be an option though since the view model wouldn't be available until my action is called.

The only other thing I could come up with was to pre-populate the menu object in the base ViewModel constructor. Then I could add/remove as necessary in my controller actions. This didn't seem entirely appropriate though since I'd be instantiating links back to controller actions in the ViewModel constructor (since each Menu item can have a controller/action/id).

Now it's real easy to do any of these, independent of your page view model:

MasterModel = this.DefaultMasterModel();

// -- or --

MasterModel = this.DefaultMasterModel();
this.AlterMenu(MasterModel.Menu);

// -- or --

MasterModel = new MasterModel();
MasterModel.Menu = this.SpecialReplacementMenu();

// -- or --

MasterModel = new NestedMasterModel();

// -- or -- 

public class FlyingPigAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var viewResult = filterContext.Result as ViewResult;

        if (viewResult!=null)
        {
            var masterModel = viewResult.ViewData["MasterModel"] as MasterModel;
            MakePigsFly(masterModel);
        }
    }
}    

While not giving one specific solution, does this open up you options as far as how you can refactor the code so it smells better?

Related Topic