开发者

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

开发者 https://www.devze.com 2023-01-16 10:29 出处:网络
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 co

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 th开发者_高级运维e 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消