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 6156163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:33:45+00:00 2026-05-23T20:33:45+00:00

Hi I would like to dynamically load meny items in the _Layout.cshtml file. How

  • 0

Hi I would like to dynamically load meny items in the _Layout.cshtml file.

How do I do that? Can you point me to a code example doing that? Or something?

I am not looking for any particular fancy way of doing this.

Below I will describe my intention:

I just want a list of different menu items rendering in the left side of the screen. Say there is a hierarchy of menu items in the form of

Categories -> Types -> Brands -> Models.

So intially just a list of the Category menu items render, then one chooses a Category and a list of Type menu items render, one choses a Type and a list of Brand menu items render, one chooses a Brand and a list of Model menu items render, like below:

(The initial appearance of the menu)

Categories:

  • Cars
  • Boats
  • Bikes
  • Motorbikes

(Cars is chosen)

Types:

  • Coupe
  • Truck
  • Van
  • Convertible

(Coupe is chosen)

Brands:

  • BMW
  • Volvo
  • Ford
  • Mercedes

(BMW is chosen)

Model:

  • 325i
  • 128i
  • 650i
  • M6
    .
  • 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-23T20:33:45+00:00Added an answer on May 23, 2026 at 8:33 pm

    If you’re talking about items specific to the controller or action, you could have the action put something in ViewData that would be read by whatever view renders the menu.

    Edit: A working example

    The data structure: a self-referential (recursive) table with the following columns:

    MenuItemId - int, primary key, identity
    DisplayName - varchar
    ParentMenuItemId - int, nullable, FK to MenuItemId
    

    I then inserted the values you identified in your OP, drilling down through Cars / Coupe / BMW.

    This is MVC3/Razor, along with an Entity Framework model.

    Controller: (the action takes a nullable int parameter named “id”, which means that MVC will automatically route it through the standard routing handler as /Controller/Action/Id)

    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    
    using (Models.StackOverflowTestEntities e = new Models.StackOverflowTestEntities())
    {
        // Insert the child menu items based on the current parent
        ViewData["currentMenuItems"] = e.MenuItems.Where(i => (!id.HasValue && !i.ParentMenuItemId.HasValue) || (i.ParentMenuItemId == id)).ToList();
    
        // Get our breadcrumb trail
        if (id.HasValue)
        {
            int? currentItem = id;
    
            while (currentItem.HasValue)
            {
                var menuItem = e.MenuItems.Where(i => i.MenuItemId == currentItem).First();
    
                // TODO: make these into actual hyperlinks?
                sb.Insert(0, string.Format(" » {0}", menuItem.DisplayName));
    
                currentItem = menuItem.ParentMenuItemId;
            }
        }
    }
    
    sb.Insert(0, "Root");
    
    ViewData["breadcrumb"] = sb.ToString();
    

    View:

    <div>@ViewData["breadcrumb"]</div>
    
    <ul>
    @{
        var menuItems = ViewData["currentMenuItems"] as IEnumerable<MvcApplication1.Models.MenuItem>;
    
        foreach (var menuItem in menuItems) {
            <li><a href="/Home/Index/@menuItem.MenuItemId">@menuItem.DisplayName</a></li>
        }
    }
    </ul>
    

    This not only shows the appropriate list of items for the specified parent, it also shows the breadcrumb trail back to the root.

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

Sidebar

Related Questions

For debugging / performance tests I would like to dynamically add logging code to
i would like to load modules dynamically and execute a method for each module
I would like to create a form that changes dynamically. I have a form
I would like to know if there is a way to dynamically load some
I would like to dynamically create a minimal transparent bar graph to display over
I would like to dynamically switch the video source in a streaming video application.
I would like to dynamically hide a button in one of my views depending
I would like to dynamically allocate memory from the machine_init function in my arm
I would like to be able to cast a value dynamically where the type
I would like to add some paragraphs or new lines or words dynamically but

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.