The scope of ViewBag is permitted to the current request and the value of ViewBag will become null while redirecting. ViewData is a dictionary object to pass the data from Controller to View where data is passed in the form of key-value pair.
In general, ViewBag is a way to pass data from the controller to the view. It is a type object and is a dynamic property under the controller base class. Compared to ViewData, it works similarly but is known to be a bit slower and was introduced in ASP.NET MVC 3.0 (ViewData was introduced in MVC 1.0).
TempData in ASP.NET MVC is basically a dictionary object derived from TempDataDictionary . TempData stays for a subsequent HTTP Request as opposed to other options ( ViewBag and ViewData ) those stay only for current request. So, TempdData can be used to maintain data between controller actions as well as redirects.
ViewData and ViewBag are used for the same purpose -- to transfer data from controller to view. ViewData is nothing but a dictionary of objects and it is accessible by string as key. ViewData is a property of controller that exposes an instance of the ViewDataDictionary class. ViewBag is very similar to ViewData.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With