9

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,,,,,

Community
  • 1
  • 1
Manik
  • 93
  • 1
  • 5

2 Answers2

12

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.

Community
  • 1
  • 1
AlexC
  • 10,676
  • 4
  • 37
  • 55
  • are they created on server?where do they stay after getting created?and furthermore if they are on server then cannot we store large data in them? – Manik Aug 06 '14 at 10:47
  • 1
    @AlexC you answer is still not clear. Where the data is stored in case of view bag ? – kbvishnu Jul 16 '15 at 03:14
  • @VeeKayBee: If that's still relevant see here: http://stackoverflow.com/questions/16871600/is-viewbag-and-viewdata-also-part-of-state-management-in-asp-net-mvc – Anytoe Aug 16 '15 at 10:57
1

Well I don't have a solid proof saying that whether viewBag or ViewData gets stored on server/client but definitely they will be stored in server memory rather in client.

First of all, no where you see people saying about their storage not even in MSDN documentation; unlike ViewState in ASP.NET where MSDN document clearly depicts that it gets stored in client side.

Second, since ViewState gets stored in client side; it uses hidden field (__VIEWSTATE) to store the viewstate data at client side.

Continuing with that, if ViewData or ViewBag also would have same kind of mechanism then viewing page source could have highlighted that but it doesn't show any such footprint.

Storing in cookies is never an option since you may never sure whether it's enabled or not at client end.

Also, does it really require storing either in server / client? Cause both ViewBag or ViewData gets popped out from memory once the view is rendered in browser. Then I don't see any reason for storing them unlike in ViewState for next page request.

Just a thought after some research.

Rahul
  • 76,197
  • 13
  • 71
  • 125