In MVC most of the interviewer ask what is TempData and what is the usage of the TempData in otherwords they ask typically like Can TempData preserve the data on every request?
 Here we see the TempData how it works and how it save the data on each request. Well the answer for the above question is YES TempData can preserve the data on next request.
   
 How the TempData persist the data,it has four ways
   
  • Not Read.
  • Normal Read.
  • Read and Keep.
  • Peek and Read.
Condition 1 (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.


Condition 2 ( 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”];

Condition 3 (Read and Keep) :- If you read the “TempData” and call the “Keep” method it will be persisted.
@TempData[“MyData”];
TempData.Keep(“MyData”);
Condition 4 ( Peek and Read) :- If you read “TempData” by using the “Peek” method it will persist for the next request.
string str = TempData.Peek("Td").ToString();




Comments

Popular posts from this blog

Asp.net- Encrypt and Decrypt connection strings in web.config file

CRM Automate build Solution using Powershell commands

Access the fields on Quick View Form – Dynamics CRM 2015