Cha*_*ran 3 asp.net-mvc tempdata
我最近一直在与TempData一个令人困惑的案件打交道:
假设TempData在以下操作中创建了:
public ActionResult MyAction1()
{
//...
myTempData = TempData["myTempData"];
//..
}
Run Code Online (Sandbox Code Playgroud)
并预期将在以下操作中使用:
public ActionResult MyAction2()
{
//...
TempData["myTempData"] = myTempData;
//..
}
Run Code Online (Sandbox Code Playgroud)
我了解,如果我调用MyAction2下一个请求,则该TempData值将被删除。但是,如果我调用其他操作,而不是MyAction2在下一个请求时会TempData被删除?如果可以的话,是否有任何技巧可以确保它在会话结束之前一直存在?
谢谢大家
在这里,您可以获取Keep和Peek Temp数据以用于下一个请求:
如果您不会阅读“温度数据”,那么它将可用于下一个后续请求
So let’s discuss these four conditions in more detail
“Tempdata helps to preserve values for a single request”.
The other half-truth which developers do not know is or I will say which confuses developer is:
“TempData CAN ALSO preserve values for the next request depending on 4 conditions”..
1. Not Read
2. Normal Read
3. Read and Keep
4. 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.
stringstr = TempData.Peek("Td").ToString();
Run Code Online (Sandbox Code Playgroud)
干杯!
| 归档时间: |
|
| 查看次数: |
1459 次 |
| 最近记录: |