Search code examples
asp.net-mvcasp.net-mvc-4viewstateviewbag

Difference of ViewBag and ViewState in Mvc C#


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


Solution

  • 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.