raklos
raklos

Reputation: 28545

prevent repopulating viewdata

Having populated a ViewData, is it possiblie to use that ViewData collection from multiple action methods within a controller without the need to repopulate it?

Upvotes: 0

Views: 54

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038940

No, ViewData is not intended to be used that way. It is only a temporary shared storage between a controller action and a view. It allows the controller to pass some model to the view. From design perspective ViewData should not be read by the controller action, it should only be written to.

You could use the Session object if you want to store objects between multiple requests or TempData (which internally uses Session) to store data between two requests.

Upvotes: 1

Adrian Zanescu
Adrian Zanescu

Reputation: 8008

yes you can if you store the data in some persistent storage between calls (like Session or some sort of cache for example). though i don't recommend that approach sometimes is necessary for long lived data that is needed everywhere and does not change very often.

Upvotes: 0

Related Questions