Please enable JavaScript.
Coggle requires JavaScript to display documents.
Pass Data to View - Coggle Diagram
Pass Data to View
ViewData
ViewData is a dictionary-like object that allows you to pass data from a controller action to its corresponding view. Like key and value pair
-
-
-
-
ViewBag
The ViewBag in MVC is one of the mechanisms to pass the data from a controller action method to a view
-
-
-
ViewBag is available only for the duration of the current request-response cycle.It will be destroyed on redirection.
TempData
Like other two(ViewBag,ViewData) temp data also help to pass data from controller to view
TempData data persists across a single redirect. Once the data is read, it’s removed from TempData
-
TempData is meant for small amounts of data, like messages or small objects, and not for large datasets.
Keys used to store and retrieve data in TempData should be strings. You should use consistent and unique keys to avoid conflicts.
TempData is typically stored in a session state, which means it incurs some storage and performance overhead associated with sessions.
Once we read the value from TempData, then the TempData key is going to be deleted from the TempData dictionary collection.
TempData["Name"] = "Pranaya"; set the value and TempData["Name"] read the value
if want to keep the values for subsequent redirection then we need to set a properties manually TempData.Keep("Name"); for partucular value and TempData.Keep() for all the items
-
ViewData,ViewBag,TempData all are weak types, The problem in this approach we don't have idea what could give the error during runtime due to types
To avoid this we have another approach call viewmodel which is strong type. we will have code editor intelligence, which will give error in case of any time related issues.Along with misplace of variable names
-