Where are VIEWDATA and VIEWBAG stored in MVC?

I have a lot of new things in MVC ... ASP.Net created a state management technique in which the viewstate or cookie was stored in the client and the session stored on the server. Similarly, we have Viewbag, ViewData and TempData in MVC (cookies and sessions also exist). I know the syntaxes, for example, from a ViewData controller stored as

ViewData ["Foo"] = "bar";

ViewBag.Foo = "bar";

and in the corresponding representation it is selected as

ViewData ["Foo"] = "bar";

@ ViewBag.Foo

All I want to know is where are the ViewData and ViewBag (client or server or somewhere else) stored? Please forgive me if this is an irrelevant question ,,,

+6
source share
2 answers

ViewBag and ViewData are part of government. These are both objects that allow you to transfer data (mainly) from the controller to the view.

This happens solely on the server side, but the idea that the data is โ€œstoredโ€ on the server is misleading. These are temporary objects that live only during the life of the HTTP request.

Use case for ViewBag and ViewData:

Transferring small amounts of data from specific locations (for example, a controller for viewing or between views). Both ViewData and ViewBag objects work well in the following scenarios:

  • Include drop-down lists of search data in an entity
  • Components such as a basket.
  • Widgets such as a user profile widget
  • A small amount of aggregated data

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

One thing you can try to avoid is to use ViewBag / ViewData. In an MVC application, the model should be what is passed to the view, and not anything else. Excessive use of ViewBag and ViewData is bad practice .

+10
source

Well, I have no convincing evidence that the viewBag or ViewData is stored on the server / client, but they will definitely be stored in the serverโ€™s memory, and not on the client.

First of all, no, where do you see what people say about their storage even in the MSDN documentation; unlike ViewState in ASP.NET , where the MSDN document clearly shows that it is stored on the client side.

Secondly, since ViewState stored on the client side; it uses a hidden field ( __VIEWSTATE ) to store viewstate data on the client side.

Continuing this, if the ViewData or viewBag also has the same mechanism, then viewing the source of the page could highlight it, but it does not display that size.

Saving in cookies is never an option, as you can never make sure that it is enabled or not on the client side.

Also, do you really need to save to the server / client? The reason that both the viewBag and the ViewData are displayed is viewBag out after viewing the view in the browser. Then I see no reason to store them, unlike ViewState for requesting the next page.

Just a thought after some research.

+1
source

Source: https://habr.com/ru/post/973405/


All Articles