I want to understand what is ViewBag and ViewState and when each of them are used. Need an explanation in code.
- 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 stackoverflow.com/questions/23623229/… 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– user28647402015-08-09 18:37:40 +00:00Commented Aug 9, 2015 at 18:37
- Did you google it before you asked the question here? There's many examples and explanations out there alreadybrroshan– brroshan2015-08-09 18:40:29 +00:00Commented Aug 9, 2015 at 18:40
- 1Here's a good one: stackoverflow.com/questions/9661171/…killthrush– killthrush2015-08-09 18:50:58 +00:00Commented Aug 9, 2015 at 18:50
- Please take a look at this: stackoverflow.com/questions/31647562/…Jamie Rees– Jamie Rees2015-08-10 08:00:13 +00:00Commented Aug 10, 2015 at 8:00
2 Answers
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.
Comments
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 .