When will the values of the ViewBag
be flushed or cleared ?
Asked
Active
Viewed 4,619 times
18

Shimmy Weitzhandler
- 101,809
- 122
- 424
- 632

Naveen Vijay
- 15,928
- 7
- 71
- 92
1 Answers
21
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.

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
Why wouldn't you recommend ViewBag? because of it live until the rending of view ? – Saad Jul 28 '11 at 12:42
-
6Because it is much nicer to have a strongly typed ViewModel. Makes testing a lot easier. – Dismissile Jul 28 '11 at 13:19
-
2Strongly type all the things! – John Farrell Jul 28 '11 at 13:32
-
4Dynamic vs static, it's just a preference thing – BlackTigerX Jul 28 '11 at 13:55
-
4If you want everything dynamic then go and use Ruby on Rails. Us C#'ers like our statically-typed goodness. :) – RPM1984 Jul 29 '11 at 09:53