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();
returnRedirectToAction(" M3" );
}
publicActionResult M3()
{
string s = TempData[" a" ].ToString();
returnview (" Some View" );
}
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" );
}
Sample 2
publicActionResult M1()
{
TempData[" a" ] = " Value" ;
returnRedirectToAction(" M2" ); }
publicActionResult M2()
{
return view(" MyView2" );
}
Sample 3
publicActionResult M1()
{
TempData[" a" ] = " Value" ;
returnRedirectToAction(" M2" ); }
}
publicActionResult M2()
{
string s = TempData[" a" ].ToString();
return view(" MyView2" );
}
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" );
}
Sample 2
publicActionResult M1()
{
TempData[" a" ] = " Value" ;
string s = TempData[" a" ].ToString();
return view(" MyView" );
}
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" );
}
publicActionResult M2()
{
string s = TempData[" a" ].ToString();
return view(" MyView2" );
}
In the above sample:
Action method M1
creates tempdata
which will be available throughout that request, but neither inside M1
nor inside MyView
(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" );
}
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" );
}
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" );
}
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();
return view (" Some View" );
}
.
.
.
publicActionResult M3()
{
string s = TempData[" a" ].ToString();
…
}
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();
return view (" Some View" );
}
.
.
.
publicActionResult M3()
{
string s = TempData[" a" ].ToString();
…
}
CodeProject Thank you for reading.
Also see our video on difference between viewdata
, viewbag
, tempdata
and session
:
VIDEO