Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 426745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:30:36+00:00 2026-05-12T19:30:36+00:00

I am trying to figure out the best way to create a navigation menu

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:30:36+00:00Added an answer on May 12, 2026 at 7:30 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out the best way to create a class that can
I'm trying to figure out the best way to create a row of three
I'm trying to figure out the best way to create three 284x87 rounded rectangle
I am trying to figure out the best way to create a view like
I am trying to figure out the best way to create a style/trigger to
I am currently trying to figure out the best way to create a .xls
I am currently trying to figure out what the best way is to create
I'm trying to figure out the best way to create a class whose sole
I'm trying to figure out the best way to validate data within a MVC
I'm using Eclipselink and I'm trying figure out the best way to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.