Friday, November 8, 2019

.NET: How To Use ViewData, ViewBag And TempData in MVC

ViewData 

It is a dictionary which can contain key-value pairs where each key must be string. We can use it in transferring data from Controller to View. We can only transfer data from Controller to view but if we wish to pass data back again to controller then it is not possible. So it is valid only for the current request.

ViewBag

ViewBag is also similar to ViewData. It is used to transfer data from Controller to View. It is a type of Dynamic object, that means you can add new fields to viewbag dynamically and access these fields in the View. You need to initialize the object of viewbag at the time of creating new fields.

TempData

TempData is a dictionary object derived from TempDataDictionary. It is for subsequent HTTP requests; unlike ViewBag and ViewData, those stay only for current request. It can be used to maintain data between controller actions as well as redirects.

REFERENCES:

No comments:

Post a Comment