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
.
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:
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)
View:
This not only shows the appropriate list of items for the specified parent, it also shows the breadcrumb trail back to the root.