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 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 it 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 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 wont 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 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 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 above sample all three tempdata values will be maintained for next request regardless of the whether values are read or not.

Sample 1 –

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 next request. Tempdata with key “c” get 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 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 above sample Tempdata values (created inside Action method M1) won’t be available for next request because it already used in current request (in Action method M2).
When user make 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 
…
}

Thank you for reading.

If you are looking for Online Training visit www.Sukesh-Marla.com. For Offline class room training visit www.stepbystepschools.net
Also see our video on difference between viewdata, viewbag, tempdata and session: –

Comments

comments

Leave A Reply

%d bloggers like this: