I want to understand what is ViewBag
and ViewState
and when each of them are used. Need an explanation in code.
Asked
Active
Viewed 3,118 times
3

Andrei
- 42,814
- 35
- 154
- 218

Asish Dash
- 51
- 1
- 6
-
View State is for *non-MVC* ASP.NET "WebForms". View State compromises state stored across post-backs via a HIDDEN field. It is *not* comparable to the View Bag in MVC which is "new each request". See http://stackoverflow.com/questions/23623229/what-is-the-equivalent-of-viewstate-in-asp-net-mvc for MVC alternatives for persisting state. Google - really the resources available that are located by such - will answer such broad non-focused questions; as such this is too broad. – user2864740 Aug 09 '15 at 18:37
-
Did you google it before you asked the question here? There's many examples and explanations out there already – brroshan Aug 09 '15 at 18:40
-
1Here's a good one: http://stackoverflow.com/questions/9661171/difference-between-viewbag-and-viewstate – killthrush Aug 09 '15 at 18:50
-
Please take a look at this: http://stackoverflow.com/questions/31647562/should-i-always-use-a-view-model-or-is-it-ok-to-use-viewdata/31647765#31647765 – Jamie Rees Aug 10 '15 at 08:00
2 Answers
2
Coming to your Question
In MVC we dont have Viewstate.In order to maintain the values refer below link
Maintaining viewstate in Asp.net mvc?
ASP.NET MVC doesn't work with ViewState and Postback?
we use ViewBag,ViewData,TempData for the flow of values from controller to view or controller to controller .

Community
- 1
- 1

Kaushik Thanki
- 3,334
- 3
- 23
- 50
2
ViewState is an old classic ASP.NET WebForms
concept when controls and ASPX pages save their state between HTTP requests using a hidden field.
ViewBag is a completely different concept in ASP.NET MVC
applications. It's just a simple container that you can use to pass some data from Controller to a View. This state doesn't live between HTTP requests.

Andrei
- 42,814
- 35
- 154
- 218