views:

56

answers:

2

In a standard ASP.NET webforms project, if I have a several user controls on a page that require the same data I'd tend to have the first user control put the data in the HttpContext.Current.Items collection so it was available for all the others.

What's the nearest equivalent to this for a ViewResult on an MVC controller that's used multiple times on the same page?

Thanks

Edit:

Basically the ViewReults here are content areas which appear more than once on a page, so I was looking to query the database once to get a collection of all the content for the page (when the first content area is laoded) rather than a separate query for each content area. Thought it would speed things up.

Further info:

Just to make it clear, the content controller action is used repeatedly as a 'RenderAction' from within pages that are served by an additional 'main' controller. Consequently I don't think I can use ViewData (or any other property on the controller class itself) as it's not shared between different instantiations of the same controller.

I think I'm going to have to fall back on the HttpContext after all using a method such as this one.

A: 

"Multiple times on the same page" means the same request meaning you can still use the HttpContext.Current.Items collection.

Developer Art
Guess so, but I thought directly accessing the HttpContext from a controller wasn't recommended as it makes unit testing harder?
Nick
A: 

Why don't you just put this content in your ViewModel and have all the controls use that? This is a pretty standard usage of a View's model.

jfar
Yes, but the content areas (served by a content controller) can appear anywhere on the website so I'd have to put the content collection in a base model as the pages themselves are served by a number of different controllers which otherwise have different model requirements.
Nick
This sounds like an appropriate use for ViewData["SomeSharedKey"]. Then you don't need a base view model.
Ryan