문제

I need some clear mind thinking to provide some good suggestion how this kind of thing should be done to its best.

Problem

I'm building an Asp.net MVC application. Most of my views have a toolbar at the top. Toolbar can have several different items on it like:

  • buttons
  • notes
  • flush items (dynamic width space that's used to put some buttons to the far right side of the toolbar)
  • etc.

To follow DRY principle I created a set of shared partial views (Toolbar, Toolbar.Button etc.) that render a predefined toolbar object. These partial views are of course strong typed.

Then I have my strong type views whose models are related to the data they display. But they quite often have a toolbar on them as well. So before calling Html.RenderPartial("Toolbar", toolbar) I have to prepare this IList<ToolbarItemBase> object list to pass it to it.

The problem is I'm preparing these in the view itself. This toolbar object list is always the same for a particular page and is also localised. Toolbar button item also has a Url property that's set using Url.Action() helper, which makes it impossible to simply store serialized toolbar instances in a database. The thing is these toolbar definitions may get really large (think of Word ribbon and the amount of items in it)

Question

What do you suggest, where should I create my localised toolbar instances without cluttering views' code? Since they don't change at all, they could most probably be static I guess...

도움이 되었습니까?

해결책

I'd consider using the Action/RenderAction helpers in ASP.NET MVC 3.

Basically the Action helper let's you set up a "child request" to a controller action that can build a model and render a partial view (and the results of the partial view will render into the outer view where you use @Html.Action(...)).

So ... in your scenario you'd have a controller action dedicated to building the view model for the toolbar, and rendering the toolbar partial. Call this action using Html.Action from the layout or content view that needs a toolbar.

Hope that helps,

다른 팁

Introduce a base model for all toolbar-enabled views and prepare the toolbar data there.

P.S. I'd probably get downvoted for that but have you considered going back to WebForms instead of MVC? If your intention is to mimic the desktop application interface with all of its functionality and complexity, MVC may not be the right tool for you. In a nutshell, MVC is for web sites, WebForms are for web applications.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top