Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are VIEWDATA and VIEWBAG stored in MVC?

Tags:

asp.net-mvc

i am very much new to MVC...In ASP .Net there was state management technique where viewstate or cookies were stored in client and session stored in server. Similarly we have Viewbag,ViewData and TempData in MVC(cookies and sessions are also there).I know the syntaxes like from controller ViewData stored as

ViewData["Foo"] = "bar";

ViewBag.Foo = "bar";

and in the corresponding view it is fetched as

ViewData["Foo"] = "bar";

@ViewBag.Foo

All I want to know is that where are ViewData and ViewBag stored(client or server or someplace else)?? Please forgive me if it is a irrelevant question,,,,,

like image 552
Manik Avatar asked Aug 06 '14 10:08

Manik


1 Answers

ViewBag and ViewData are part of state management. They are both objects that allow passing of data (mainly) from the Controller to the View.

This happens entirely on the server side but the idea that the data is "stored" on the server is misleading. These are transient objects which only live for the lifetime of the HTTP request.

The use case for ViewBag and ViewData is:

transporting small amounts of data from and to specific locations (e.g., controller to view or between views). Both the ViewData and ViewBag objects work well in the following scenarios:

  • Incorporating dropdown lists of lookup data into an entity
  • Components like a shopping cart
  • Widgets like a user profile widget
  • Small amounts of aggregate data

from http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

One thing to try and avoid is overusing ViewBag / ViewData. In a MVC application the model should be the thing that is passed to the view rather than anything else. Overuse of ViewBag and ViewData is a poor practice.

like image 166
AlexC Avatar answered Sep 17 '22 14:09

AlexC