Understanding TempData in Detail





5.00/5 (2 votes)
Understanding TempData in detail
What is TempData?
Tempdata
let us maintain data within single request cycle.
Example
publicActionResult M1()
{
TempData["a"] = "Value";
string s = TempData["a"].ToString();
returnRedirectToAction("M2");
}
publicActionResult M2()
{
string s = TempData["a"].ToString(); // Data will be available
returnRedirectToAction("M3");
}
publicActionResult M3()
{
string s = TempData["a"].ToString(); // Data will be available
returnview ("Some View"); // Data will be available inside view also
}
Can We Use Tempdata to Pass Data from Controllers to View?
Many people says No, but the reality is YES.
Example
Sample 1
publicActionResult M1()
{
TempData["a"] = "Value";
return view("MyView"); // MyView can access Tempdata
}
Sample 2
publicActionResult M1()
{
TempData["a"] = "Value";
returnRedirectToAction("M2"); }
publicActionResult M2()
{
return view("MyView2");// MyView2 can access Tempdata
}
Sample 3
publicActionResult M1()
{
TempData["a"] = "Value";
returnRedirectToAction("M2"); }
}
publicActionResult M2()
{
string s = TempData["a"].ToString(); // Data will be available
return view("MyView2");// MyView2 can access Tempdata
}
When the Tempdata Values Get Removed?
- At the end of the request, values in the
tempdata
get removed automatically considering values are marked for deletion. - Values will be marked for deletion as soon as we read it.
Example
Sample 1
publicActionResult M1()
{
TempData["a"] = "Value";
return view("MyView"); // MyView can access Tempdata
}
Sample 2
publicActionResult M1()
{
TempData["a"] = "Value";
string s = TempData["a"].ToString(); // Data will be available
return view("MyView"); // MyView can access Tempdata
}
Note: In the above two samples, values are marked for deletion as soon they get read. Now for the next request, values won’t be available.
Sample 3
publicActionResult M1()
{
TempData["a"] = "Value";
return view("MyView"); // MyView can access Tempdata but it wont access it
}
publicActionResult M2()
{
string s = TempData["a"].ToString(); // Data will be available
return view("MyView2"); // MyView can access Tempdata
}
In the above sample:
- Action method
M1
createstempdata
which will be available throughout that request, but neither insideM1
nor insideMyView
(which is the view here) values are read and hence values won't get removed at the end of the request. - When user makes a fresh request to
M2
values created in the last request will be available and the cycle continues. Means values will get removed at the end of this second request considering it will be read before request ends.
Keep and Peek Methods
Keep
Keep
method makes the values in the tempdata
persistent for the next request.
Example
Sample 1
publicActionResult M1()
{
TempData["a"] = "Value";
TempData["b"] = "Value1";
TempData["c"] = "Value2";
TempData.Keep();
return view("MyView"); //My View can access all three tempdata values and even it will access
}
In the above sample, all three tempdata
values will be maintained for next request regardless of whether values are read or not.
Sample 2
publicActionResult M1()
{
TempData["a"] = "Value";
TempData["b"] = "Value1";
TempData.Keep();
TempData["c"] = "Value2";
return view("MyView"); //Let say My View displays all three tempdata values
}
In the above sample, only Tempdata with key “a
” and “b
” will be available for the next request. Tempdata
with key “c
” gets removed considering it was used inside view.
Sample 3
publicActionResult M1()
{
TempData["a"] = "Value";
TempData["b"] = "Value1";
TempData["c"] = "Value2";
TempData.Keep("a");
return view("MyView"); //My View can access all three tempdata values and even it will access
}
In the above sample, only tempdata
with key “a
” will be made available. Availability of tempdata
with key b
and c
depends on the criteria “Whether value was used in the current request or not”.
Peek
Peek will let us retrieve the tempdata
value without marking it for deletion.
Example without Peek
publicActionResult M1()
{
TempData["a"] = "Value";
returnRedirectToAction("M2");
}
publicActionResult M2()
{
string s = TempData["a"].ToString(); // Data will be available
return view ("Some View"); // Data will be available inside view also
}
.
.
.
publicActionResult M3()
{
string s = TempData["a"].ToString(); // Will get an Exception
…
}
In the above sample, Tempdata
values (created inside Action method M1
) won’t be available for the next request because it is already used in current request (in Action method M2
).
When user makes a fresh request to action method M3
, null
reference exception will be thrown.
Example with Peek
publicActionResult M1()
{
TempData["a"] = "Value";
returnRedirectToAction("M2");
}
publicActionResult M2()
{
string s = TempData.Peek("a").ToString(); // Data will be available
return view ("Some View"); // Data will be available inside view also but won’t be used
}
.
.
.
publicActionResult M3()
{
string s = TempData["a"].ToString(); // Will just work
…
}
CodeProjectThank you for reading.
Also see our video on difference between viewdata
, viewbag
, tempdata
and session
: