MVC TempData – Peek vs Keep

Most of the MVC developer knows that TempData is used to preserve data for a single request but reality is that TempData can pass data for next request also. In this article we will discuss how to persist data with TempData using Keep and Peek method as well we will also see the difference between Keep and Peek method.

Normally MVC developer knows that TempData is used to preserve data for a single request. This request can traverse through multiple actions or controllers until it displays the view on the browser:




But the reality is that in the same session (without closing the browser), if a new or second request is initiated then "TempData" may be persisted but it depends on 4 conditions:

  1. Not Read
  2. Normal Read
  3. Read and Keep
  4. Peek and Read

Let me describe all these conditions in detail:

  1. Not Read
    If you set a TempData in your action method and if you do not read it in your view then TempData will be persisted and will be available in next request.
  2. Normal Read

    If you set a TempData in your action method and if you read it in your view then TempData will not be persisted and will not be available in next request.

    You may use TempData to display its data directly.

    @TempData["Message"];

    You may fetch TempData in your view and store in a variable.

    string message = TempData["Message"];
  3. Read and Keep

    If you set a TempData in your action method and if you read it in your view and call the "Keep" method then TempData will be persisted and will be available in next request.

    We can save TempData value using keep method after reading value from TempData. There are two overloads for Keep method.

    void Keep()

    This method you can use when all items in TempData you want to retain and does not allow deletion for any TempData’s items.

    @TempData["Message"];
    TempData.Keep();

    void Keep(string key)

    This method you can use when particular TempData’s value you want to persist and does not allow deletion for that particular TempData’s value. Rest of the TempData’s values will be deleted.

    @TempData["Message"];
    TempData.Keep("Message");

    You can understand it by this way, by calling "Keep" method, you are specifying that keep (persist) this TempData for next request.

  4. Peek and Read

    If you set a TempData in your action method and if you read it in your view by calling "Peek" method then TempData will be persisted and will be available in next request.

    string message = TempData.Peek("Message").ToString();

    Peek method is doing both tasks here in same statement: reading and persisting data.

    This diagram will describe about all these conditions:


Difference between Keep and Peek Method

  1. Marking for Deletion

    When an object in a TempDataDictionary is read, it is marked for deletion but we can use Keep and Peek method to save Data with TempData.

    • Once we retrieved value from object than it is marked for deletion, with Keep method we can later save object from Deletion. It means first deletion marking is happened then saving using Keep method.
    • With Peek method we can retain TempData value without marking for deletion in a single call. It means deletion marking is not happening in case of Peek method. It directly persist TempData.
  2. Saving Specific TempData
    • Keep provides 2 overload methods. One can save particular TempData on condition based and second can save all TempData’s value.
    • Peek method always saves particular TempaData’s value