TempData
ViewBag or ViewData can be passed only from a controller's action to view, its not passed from one action to another action.
You can redirect the control from one action to another action using the method RedirectToAction(<<actionName>>, <<controller>>)
Syntax of TempData is very similar to ViewData
Using TempData in View
To maintain data form complete request i.e. from one action to another to the view, use TempData.
Peek and Keep
TempData helps to maintain data to preserve or persist data for the next request if any of the below 3 conditions is met:
Lets say we have a value "Val1" in TempData set in some Controller action:
If the Tempdata value is not used or read in View, the value in the TempData would persist in the next session request from client to next to next until its value is read in some view.
If the Tempdata value is read in a View and then
Temp.Keep("Val1")
is called, then the value of TempData would be preserved for the next session request.View code:
If instead of reading the value of TempData in line in the HTML, is its value is used using
TempData.Peek("Val1")
, then TempData value would be preserved for the next session request.
View code:
Last updated
Was this helpful?