Click here to Skip to main content
15,891,204 members
Articles / All Topics

Understanding TempData in Detail

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Sep 2014CPOL2 min read 10.8K   2   3
Understanding TempData in detail

What is TempData?

Tempdata let us maintain data within single request cycle.

Example

C#
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

C#
publicActionResult M1()
{
TempData["a"] = "Value";
return view("MyView"); // MyView can access Tempdata
}

Sample 2

C#
publicActionResult M1()
{
TempData["a"] = "Value";
returnRedirectToAction("M2"); }

publicActionResult M2()
{ 
return view("MyView2");// MyView2 can access Tempdata
}

Sample 3

C#
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

C#
publicActionResult M1()
{
TempData["a"] = "Value";
return view("MyView"); // MyView can access Tempdata
}

Sample 2

C#
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

C#
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 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
C#
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
C#
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
C#
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

C#
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

C#
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.

Also see our video on difference between viewdata, viewbag, tempdata and session:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Just Compile
India India
Learning is fun but teaching is awesome.

Who I am? Trainer + consultant + Developer/Architect + Director of Just Compile

My Company - Just Compile

I can be seen in, @sukeshmarla or Facebook

Comments and Discussions

 
QuestionNice! Pin
Volynsky Alex15-Sep-14 10:24
professionalVolynsky Alex15-Sep-14 10:24 
AnswerRe: Nice! Pin
Marla Sukesh16-Sep-14 4:27
professional Marla Sukesh16-Sep-14 4:27 
QuestionRe: Nice! Pin
Volynsky Alex16-Sep-14 9:09
professionalVolynsky Alex16-Sep-14 9:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.