ViewData and ViewBag in MVC

ViewData

Data travels from controller to the view via a ViewDataDictionary. This ViewDataDictionary is a dictionary class;
we call this class- ‘ViewData’

Ex:

ViewData["Message"] = “Say Hello to C# Corner”;

(This will give you a message- ‘Say Hello to C# Corner’)

 

ViewBag

ViewBag is just a dynamic wrapper around ViewData. With it you don’t need to write dynamic keyword, it takes
dynamic keyword internally.

We often call ViewBag- ‘A dynamic data library’

Ex:

ViewBag.Message = “Say Hello to C# Corner”;

(This will give you a message- ‘Say Hello to C# Corner’)

 
Ebook Download
View all
Learn
View all