3

I want to understand what is ViewBag and ViewState and when each of them are used. Need an explanation in code.

4
  • 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. Commented Aug 9, 2015 at 18:37
  • Did you google it before you asked the question here? There's many examples and explanations out there already Commented Aug 9, 2015 at 18:40
  • 1
    Here's a good one: stackoverflow.com/questions/9661171/… Commented Aug 9, 2015 at 18:50
  • Please take a look at this: stackoverflow.com/questions/31647562/… Commented Aug 10, 2015 at 8:00

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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 .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.