What is difference between ViewData and ViewBag in ASP.NET MVC?

What is difference between ViewData and ViewBag in ASP.NET MVC?



-ViewData is a dictionary object in which we can store the data. The data then will be accessed from the view.

-ViewData is derived from the ViewDataDictionary class and we can use the traditional "key-value" syntax.

-The ViewBag object act as a wrapper around the ViewData object.

-By using ViewBag we can create dynamic properties for the ViewBag.

-For example
ViewData["product"] = product; // Syntax of ViewData
ViewBag. product = product; // Syntax of ViewBag
Post your comment