Sourabh Somani
What is the difference between TempData keep() and peek() function?
By Sourabh Somani in ASP.NET on Jun 19 2017
  • Rajiv Bhardwaj
    Aug, 2017 17

    Cool... The query is so simple. Why to make its answer complex. Lemme try to explain in simple words. TempData Keep() & Peek(): Most of the preliminary developers know the basic thing about this is: "TempData is used to preserve the data for next request".... i.e. Once the data is read by the view its lost, and not available for next request...... This is true but its half truth. The other half is: The data can be retained & preserved for next request, and here comes Keep() & Peek() Both Keep() & Peek() is used to tell the server to keep this data for next request even if its read by view. This was the basic knowledge.You can read further if wanna know in detail. In detail: There are mainly 4 conditions when data is/can be preserved: 1. Is the data NOT read by view 2. Is the data READ by view 3. data read and its marked as Keep() 4. data marked as Peek() and then its readThe above 4 conditions are quite clear. 1. Is the data NOT read by view: Data will be available for next request if its not read. 2. Is the data READ by view: Data will not be available for next request if its read by view. 3. data read and its marked as Keep(): data will be available for next request if its marked as Keep() after reading the data. 4. data marked as Peek() and then its read: Data will be available for next request if ts marked as Peek() and then read.Note: something we noted here is that, Keep is used after reading the data where as Peek() is used before reading data. e.g.: @TempData["userName "]; <---- read the data TempData.Keep("userName "); <--- marked with Keep() --------- var userName = TempData.Peek("userName ").ToString();Thanks & Enjoy :-)

    • 12
  • Ankit Saraf
    Jul, 2017 12

    temdata uses TempDataDictionary where every data read, will be marked as available for deletion, keep() and peek() both are used to retain data, the only difference is, keep() will be used to retain data that has been marked for deletion (Read and Keep), whereas peek() will be used where you want to read data without marking it for deletion. Example:@TempData["MyData"]; TempData.Keep("MyData");here we are reading it first after that unmarking it using keep.stringstr = TempData.Peek("Td").ToString();here we are reading without marking it for deletion.

    • 3
  • Amit Verma
    Aug, 2017 16

    temdata uses TempDataDictionary where every data read, will be marked as available for deletion, keep() and peek() both are used to retain data, the only difference is, keep() will be used to retain data that has been marked for deletion (Read and Keep), whereas peek() will be used where you want to read data without marking it for deletion.

    • 2
  • Satheesh Elumalai
    Oct, 2017 10

    Once TempData is read the current reques it's not available in the subsequents request.if we want TempData to be read and also avalaible in the subsequents request then after reading we need to call keep method.the more shortcut way to achieving the same is by using peek this function helps to read as well advices MVC to maintain TampData for the subsequents request.

    • 1
  • Ankur Mistry
    Sep, 2017 25

    http://www.c-sharpcorner.com/UploadFile/ansh06031982/using-tempdata-peek-and-keep-in-Asp-Net-mvc/

    • 1
  • Nazmul Badsha
    Sep, 2017 12

    AS as like

    • 1
  • raj patil
    Sep, 2017 1

    TempData Keep() & Peek(): Most of the preliminary developers know the basic thing about this is: "TempData is used to preserve the data for next request".... i.e. Once the data is read by the view its lost, and not available for next request...... This is true but its half truth. The oth

    • 1
  • Golden Memory
    Aug, 2017 17

    I Created Most Frequently Asked .NET Interview Questions and Answers for android App Offline. please have a look and Install the app for quick guide. Hope it will add more value to job seeker => https://play.google.com/store/apps/details?id=com.profuia.crackinterview

    • 1
  • Amit Verma
    Aug, 2017 16

    temdata uses TempDataDictionary where every data read, will be marked as available for deletion, keep() and peek() both are used to retain data, the only difference is, keep() will be used to retain data that has been marked for deletion (Read and Keep), whereas peek() will be used where you want to read data without marking it for deletion.

    • 1
  • Gokhul Varman
    Aug, 2017 15

    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:Not Read Normal Read Read and Keep Peek and Read Let me describe all these conditions in detail: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. 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"]; 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.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 MethodMarking 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. 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

    • 1
  • sushil kumar
    Aug, 2017 12

    using keep() first read temp date then keep for tempdata for next read. while peek() is used for read and keep simultaneously.

    • 1
  • Vivek Sheth
    Jul, 2017 6

    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.For more details, Please read below link : https://hassantariqblog.wordpress.com/2016/09/02/mvc-when-to-use-keep-vs-peek-in-asp-net-mvc/

    • 1
  • Sourabh Somani
    Jul, 2017 6

    If both are same then why we have two methods ????

    • 1
  • Abhinandan Nimsarkar
    Jul, 2017 5

    If you read the “TempData” and call the “Keep” method, it will be persisted. If you read “TempData” by using the “Peek” method, it will persist for the next request.

    • 1
  • Pawan T
    Jul, 2017 5

    keep() and peek() both are used to retain value after reading.Generally after reading value it gets deleted.but if you call them on tempdata it maintain value for another request .

    • 1
  • React Hunt
    May, 2024 22

    I discovered Gizmogo when looking for a solution to tidy my old electronics, and I must say that I’m very impressed. Selling used electronic devices is made simple by your platform. Your workflow runs smoothly and efficiently across smartphones and tablets. What I value most is your dedication to sustainability. It’s nice to know that my old equipment will be responsibly recycled or restored rather than being discarded in landfills. Furthermore, the extra cash in exchange is a welcome bonus! Thank you for offering such a vital service and supporting environmentally responsible practices. I’ll absolutely recommend Gizmogo to friends and family wanting to get rid of their equipment.

    • 0
  • Rajeev Kumar
    Jun, 2023 29

    Once TempData is read the current reques it’s not available in the subsequents request.if we want TempData to be read and also avalaible in the subsequents request then after reading we need to call keep method.

    • 0
  • Tuhin Paul
    Feb, 2023 22

    In ASP.NET MVC, TempData is a dictionary that is used to pass data between two consecutive requests, such as between an action method and a view. The TempData dictionary is used to store data for only one subsequent request and then automatically cleared.

    The keep() and peek() functions are used to manipulate the data stored in the TempData dictionary.

    The keep() function is used to keep the data in the TempData dictionary for the next request. When the keep() function is called, the data stored in the TempData dictionary is marked for retention for the next request, and it will not be automatically cleared.

    For example, the following code stores a message in TempData and marks it for retention for the next request:

    1. TempData["Message"] = "Hello World!";
    2. TempData.Keep("Message");

    The peek() function, on the other hand, is used to read the data from the TempData dictionary without marking it for retention for the next request. When the peek() function is called, the data stored in the TempData dictionary is read but not removed, and it will still be automatically cleared after the next request.

    For example, the following code reads a message from TempData using peek():

    1. string message = TempData.Peek("Message") as string;

    • 0
  • lisa li
    Nov, 2022 4

    TempData helps to maintain data when we want to transfer data from one action method to slope io another action method of the same or a different controller as well as redirects.

    • 0
  • Sam Aviles
    Oct, 2022 13

    Peek() allows you to read an item without marking it for deletion while Keep() allows you to Revive either all values in the TempData dictionary (using Keep()) or a single item (using Keep(“Key”)) that’s been marked for deletion. I use Keep() when there’s a specific criteria in the logical flow that creates the need to retain TempData that should be otherwise (under normal circumstances) cleared. e.g.: var query = TempData[“Query”]; / then somewhere down the road / if(keepQueryForSomeReason) TempData.Keep(“Query”); // this revives Query from “marked for deleted” state
    best wireless earbuds

    • 0
  • Jez Supreme
    Jun, 2022 7

    I can’t get really what the difference is, don’t they both keep a value for another request?
    boring ads

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS