ViewBag/ViewData Lifecycle
Asked Answered
B

4

40

I have seen many posts about when to use ViewBag/ViewData vs ViewModel but i have not been able to find an explanation of the lifecycle of the ViewBag.

For example, i have two Action methods in one Controller:

// POST: /MyModel/Edit/5
[HttpPost]
public ActionResult Edit(MyModel _mymodel){}

and

// GET: /MyModel/Edit/5
public ActionResult Edit(int id){}

If i put some values in the ViewBag in the GET action method, to set up some Form labels, then when they user clicks 'Submit' button and the Form is posted back to the server via HTTP POST, the ViewBag values are no longer within the POST action method.

Can someone please explain (or provide reference to good article) the lifecycle of the ViewBag/ViewData ?

Beginner answered 8/2, 2012 at 1:34 Comment(0)
M
42

The data you put in the ViewBag/ViewData is only available during the life-cycle of the request within which you populated it. MVC does not have post backs. If you need something to persist over more than a single request, you should use Session.

Here is a decent article about the differences between ViewData, ViewBag, and TempData: http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

Middleton answered 8/2, 2012 at 2:1 Comment(2)
thanks for the reply. I had read that article and it does not touch on the lifecycle of ViewBag/ViewData but does slightly on TempData. To clear things up, by 'Post Back' all i meant was user submits a FORM thereby causing a HTTP POST Request, which is then handling by a Controllers appropriate Action method.Beginner
The article does state "However, once the controller redirects, the ViewBag and ViewData will contain null values." Correct, she doesn't specifically say that the ViewBag and ViewData's life-cycle ends once the request is complete, but she does imply it.Middleton
U
13

The accepted answer here doesn't really describe the lifecycle of ViewBag/ViewData. It's unfortunate there appears to be no clear documentation about this. However, based on this:

http://blogs.msdn.com/b/varunm/archive/2013/10/03/understanding-of-mvc-page-life-cycle.aspx

It would seem the lifecycle is:

IIS request -> Routing -> MVC Handler -> Controller (with ViewData) -> View (with ViewData) -> Disposal

So, the ViewData (which ViewBag simply wraps) would actually be instantiated with the ControllerContext, at the same time TempData is instantiated. This occurs a few steps after Step 4: MVC Handler Executes.

There's an interesting step later where "If the Page has ViewData, the ViewData is set" during the handoff from Controller to View. ViewData is clearly available prior to this, so set can't mean instantiate. It appears to instead mean it's transferred from the Controller (which remember isn't available to a View) to the ViewContext (the container that provides the View access to ViewBag/ViewData, and Model).

The ViewData is presumably disposed of at the same time as the rest of the View.

It's important to also note that MVC Views are rendered from the inside out, so the particular View and any assignments it makes to the ViewBag will occur likewise in the order of inside to outside. That means something set on a View child page will be available to a Layout, but adding something to a ViewBag in a Layout and then reading it in a View child page will fail.

Ulane answered 28/1, 2014 at 4:25 Comment(0)
K
2

From MSDN - ViewBag: The dynamic view data dictionary, ViewData: The dictionary for the view data.

So these/this is a dictionary for a given view. You set its values in your action and you use it in your view. As Zach said it's not coming back with the subsequent request. You can send its values back to any given action as a form field, in querystring, etc, but these values won't be automatically available as VieBag's properties.

Kowal answered 8/2, 2012 at 3:41 Comment(0)
K
0

ViewBag and ViewData are used for the same purpose. They are used to pass data from controllers to the View. When we assign any data or object to them they are accessible in the View.

  • ViewData: ViewData is a dictionary of objects and they are accessible by string as key.
  • ViewBag: Uses the dynamic feature. It allows an object to add dynamic properties to it.
Keelson answered 23/7, 2012 at 12:58 Comment(1)
This is not true. Why do you think there are 2? ViewBag is a dynamic property bag for the view itself, such as page title, localization data, such as labels and may change if your controller action, renders different views, such as web/mobile etc and should users switch language. ViewData is the Model, the data being passed to the view to populate the page with data, such as populating input controls with values, or some user response data.Equinoctial

© 2022 - 2024 — McMap. All rights reserved.