Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lifetime of ViewBag elements in ASP.net MVC3

When will the values of the ViewBag be flushed or cleared ?

like image 226
Naveen Vijay Avatar asked Jul 28 '11 12:07

Naveen Vijay


People also ask

What is the lifetime for TempData?

The lifespan of TempData is rather unusual: It lives for one request only.

Which is faster ViewData or ViewBag?

ViewBag will be slower than ViewData; but probably not enough to warrant concern.

Is the performance of ViewBag slower than ViewData?

In general, ViewBag is a way to pass data from the controller to the view. It is a type object and is a dynamic property under the controller base class. Compared to ViewData, it works similarly but is known to be a bit slower and was introduced in ASP.NET MVC 3.0 (ViewData was introduced in MVC 1.0).

How do I debug ViewBag?

Click your ViewBag. MyData and hit F9 . When debugging, once the break-point is hit, hover your mouse over the ViewBag to see its contents.


1 Answers

When you leave the view on subsequent request. ViewBag is created in the controller and it will live until the rendering of the view. In addition to this it is something that I would not recommend you using and replace it with view models.

like image 89
Darin Dimitrov Avatar answered Oct 21 '22 02:10

Darin Dimitrov