Nick
Nick

Reputation: 5696

Shared 'per request' storage for ASP.NET MVC controllers

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.

Upvotes: 0

Views: 899

Answers (2)

John Farrell
John Farrell

Reputation: 24754

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.

Upvotes: 0

user151323
user151323

Reputation:

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

Upvotes: 1

Related Questions