user1137993
user1137993

Reputation: 101

Render a dynamic menu in _layout.cshtml

I am working on a MVC3 application with entity framework and SQL Server 2008. I want to render a dynamic menu in _layout.cshtml file using data in my database (best selling products) and ul and li HTML tags. Normally, views can receive a model object, but _Layout file doesn't. So I'm wondering how I could pass the data I need for rendering the menu to _Layout.

Upvotes: 1

Views: 7368

Answers (1)

JimmiTh
JimmiTh

Reputation: 7449

The best approach, in my opinion, is to use RenderAction from your _layout.cshtml. That action can then use any model you want to render the menu.

ETA: Example here: https://stackoverflow.com/a/4624417/1169696

Another approach would be to derive all your models from a base model, and then use that base model as the model for _layout. This has the downside of having to specify a model in all your views, even when they don't actually need one themselves (and of course, having to derive all models from a base model might be seen as a downside in itself).

Upvotes: 2

Related Questions