MVC Tempdata. Peek and Keep

MVC Tempdata. Peek and Keep.

“TempData CAN ALSO preserve values for the next request depending on 4 conditions”.

“TempData” will be persisted depending on 4 CONDITIONS:-

  • Not Read.
  • Normal Read.
  • Read and Keep.
  • Peek and Read.

(Not read):- If you set a “TempData” inside your action and if you do not read it in your view then “TempData” will be persisted for the next request.

 

( Normal Read) :- If you read the “TempData” normally like the below code it will not persist for the next request.

 

stringstr = TempData[“MyData”];

Even if you are displaying it’s a normal read like the code below.

@TempData[“MyData”];

 

When an object in a TempDataDictionary is read, it will be marked for deletion at the end of that request.

That means if you put something on TempData like

TempData[“value”] = “someValueForNextRequest”;

And on another request you access it, the value will be there but as soon as you read it, the value will be marked for deletion:

//second request, read value and is marked for deletionobject value = TempData[“value”];

//third request, value is not there as it was deleted at the end of the second requestTempData[“value”] == null

The Peek and Keep methods allow you to read the value without marking it for deletion. Say we get back to the first request where the value was saved to TempData.

With Peek you get the value without marking it for deletion with a single call

//second request, PEEK value so it is not deleted at the end of the requestobject value = TempData.Peek(“value”);

//third request, read value and mark it for deletionobject value = TempData[“value”];

With Keep you specify a key that was marked for deletion that you want to keep. Retrieving the object and later on saving it from deletion are 2 different calls. See msdn

//second request, get value marking it from deletionobject value = TempData.[“value”];//later on decide to keep itTempData.Keep(“value”); //third request, read value and mark it for deletionobject value = TempData[“value”];

You can use Peek when you always want to retain the value for another request. Use Keep when retaining the value depends on additional logic.

 

TempData won’t destroy in the below case when check for the value filterContext.Controller.TempData.ContainsKey(“value”)

Only if you read it then it will distroy:

var value = filterContext.Controller.TempData[“value”];

You could manually evict the TempData value if you want:

filterContext.Controller.TempData.Remove(“value”);

 

 

 

Advertisements

2 comments on “MVC Tempdata. Peek and Keep

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s