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
Condition 2 ( Normal Read) :- If you read the “TempData” normally like the below code it will not persist for the next request.
Condition 3 (Read and Keep) :- If you read the “TempData” and call the “Keep” method it will be persisted.
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 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.
Hide Copy Code
@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
Post a Comment