English 中文(简体)
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

最佳回答

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.

问题回答

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.

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.

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.

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.

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.

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





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签