gpt4 book ai didi

asp.net-mvc - 临时数据持久化

转载 作者:行者123 更新时间:2023-12-02 13:16:49 26 4
gpt4 key购买 nike

我最近一直在使用 TempData 并面临一个令人困惑的情况:

假设TempData是在以下Action中创建的:

public ActionResult MyAction1()
{
//...
myTempData = TempData["myTempData"];
//..
}

预计将在以下操作中使用:

public ActionResult MyAction2()
{
//...
TempData["myTempData"] = myTempData;
//..
}

据我所知,如果我在下一个请求中调用 MyAction2TempData 值将被删除。但是,如果我在下一个请求时调用其他操作,而不是 MyAction2TempData 是否会被删除?如果是的话,有什么技巧可以确保它存在直到 session 结束吗?

谢谢大家。

最佳答案

在这里,您可以为下一个请求保留并查看临时数据:

如果您不读取临时数据,那么它将可用于下一个后续请求

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();

关于asp.net-mvc - 临时数据持久化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45096482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com