What is the difference between ViewData, ViewBag & TempData?

ViewData being a dictionary object is accessible using strings as keys and also requires typecasting for complex types. On the other hand, ViewBag doesn't have typecasting and null checks. TempData is also a dictionary object that stays for the time of an HTTP Request.

ViewData, ViewBag, and TempData are all ways to pass data between a controller and a view.

ViewData and ViewBag are used to transfer data from controller to view.

TempData is used to pass data from controller to controller(pass data between two action methods).

ViewBag doesn’t require typecasting, whereas ViewData require the typecasting.


 

Post a Comment

Previous Post Next Post