LiamB
LiamB

Reputation: 18586

Asp.net MVC Dynamic Menu System

In our current Asp.net MVC application we have 2 menu systems one accross the top and one on the left hand side. Now we have a partial view that renders the menu, however is the only way for this too work to return the menu items back with every single ViewModel? We are trying not to use the ViewData dictionary.

I think the answer to this is yes, however I want to see what others think

Upvotes: 5

Views: 4566

Answers (7)

LiamB
LiamB

Reputation: 18586

Just to add to this, RenderAction was added to MVC2 Beta.

Upvotes: 0

Ryan Lanciaux
Ryan Lanciaux

Reputation: 5976

You could use the Render Action from the MVC Future's library if you want to have your own controller, etc. for your menus apart from using the main view model.

Upvotes: 3

Josh Pearce
Josh Pearce

Reputation: 3455

Are you using a Base Controller? I have found that using one, and overriding the OnActionExecuting method help me to have a central place to keep all my common page logic.

Upvotes: 3

PatrickSteele
PatrickSteele

Reputation: 14677

Use an ActionFilter to populate ViewData with the menu information you need. Apply it only to the classes and/or methods (possibly on a base Controller class if it's needed everywhere). Create some extension methods on Controller that make accessing the data from ViewData strongly-typed (and transparent in case you change its storage location later).

I recently did a blog post with a similar approach (I needed a list of Sponsors to display on every page). It may help point you in the right direction.

Upvotes: 0

Tony Heupel
Tony Heupel

Reputation: 1053

There are a lot of options for handling this, but there is one very straightforward way we have found to handle this that does not involve re-architecting your whole app.

We have had similar issues, where our page/partial has a well-defined ViewData.Model type, but the view contains a partial that is re-used across multiple pages. We also have tried to avoid using ViewDataDictionary as well.

However, we found that exactly the case you describe is the EXACT scenario where we like to use a ViewDataDictionary entry. We keep a static Constants class in our application model that contains inner classes for each type of constant, including ViewData keys so that we don't have strings for these things floating everywhere.

Then, our Controller action populates the ViewData key and the partial inside the other page/partial checks for the existence of that key and uses that instead of ViewData.Model. It makes the partial work anywhere that it needs to and keeps your ViewModel clean. By using constants, we avoid raw strings everywhere.

Upvotes: 0

mxmissile
mxmissile

Reputation: 11673

3 Options:

  1. RenderAction all the way.

  2. RenderPartial as Ryan answered.

  3. An abstract MasterViewModel for example. All your out model's would inherit from this. Populated by an action filter.

Upvotes: 3

user151323
user151323

Reputation:

You basically have two options:

  1. Use the ViewModel to set up menu items to display, they will be accessible from any view, full or partial.

  2. Create an hierarchy of strongly typed models, put menu items somewhere into the BaseModel, the will be then present in each and every derived model.

The thing you may interested in is asynchronous controllers or partial requests. It's not implemented in ASP.NET MVC, but you could check the MVC Contrib community project, it has some support with it.

Upvotes: 2

Related Questions