为什么 TempData[] 不适用于 IE

发布于 2024-12-11 17:39:30 字数 496 浏览 0 评论 0原文

在我的 MVC3 项目中,我使用大量 TempData[] 在操作之间传递数据。当我使用 Chrome 时,它​​的效果非常完美。但在 IE 中我无法获取 TempData[] 项的值。如果有人知道问题是什么以及我该如何解决它?`

public class SomeController : Controller
{
    public ActionResult SomeAction()
    {
        TempData["id"] = "someData";
        return View();

    }
}


public class AnotherController : Controller
{
    public ActionResult AnotherAction()
    {
        string data = Convert.ToString(TempData["id"]);
        return View();

    }
}

`

İn my MVC3 project, there is plenty of TempData[] that I am using for passing datas between actions. And it works totaly perfect when I use Chrome. But in IE I can't get values of TempData[] items. if anyone knows whats the problem and how can I solve it?`

public class SomeController : Controller
{
    public ActionResult SomeAction()
    {
        TempData["id"] = "someData";
        return View();

    }
}


public class AnotherController : Controller
{
    public ActionResult AnotherAction()
    {
        string data = Convert.ToString(TempData["id"]);
        return View();

    }
}

`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-12-18 17:39:30

您永远不应该从将某些内容存储到 TempData 的控制器操作返回视图。您应该立即重定向到应该使用它的控制器操作:

public class SomeController : Controller
{
    public ActionResult SomeAction()
    {
        TempData["id"] = "someData";
        return Redirect("AnotherAction", "Another");
    }
}


public class AnotherController : Controller
{
    public ActionResult AnotherAction()
    {
        string data = Convert.ToString(TempData["id"]);
        return View();
    }
}

原因是 TempData 仅在单个附加请求中存在。因此,例如,如果在视图内向某个控制器操作(无论是哪个)发送 AJAX 请求,然后在此视图中有一个指向目标操作的链接,则当用户重定向到此目标操作时,TempData 将不再存在因为它在之前完成的 AJAX 请求期间丢失了。

如果您需要存储数据的时间长于单个重定向,则可以使用 Session。

You should never return a view from a controller action that stores something into TempData. You should immediately redirect to the controller action that is supposed to use it:

public class SomeController : Controller
{
    public ActionResult SomeAction()
    {
        TempData["id"] = "someData";
        return Redirect("AnotherAction", "Another");
    }
}


public class AnotherController : Controller
{
    public ActionResult AnotherAction()
    {
        string data = Convert.ToString(TempData["id"]);
        return View();
    }
}

The reason for this is that TempData survives only for a single additional request. So for example if inside the view you are sending an AJAX request to some controller action (no matter which) and then have a link in this view pointing to the target action, when the user is redirected to this target action TempData will no longer exist since it was lost during the AJAX request done previously.

If you need to store data for longer than a single redirect you could use Session.

三五鸿雁 2024-12-18 17:39:30

如果您需要存储数据的时间长于单个重定向,则应使用 Keep 或 Peek 方法。

string data = TempData["id"].;
TempData.Keep("id");

或者简单地使用

string data = TempData.Peek("id").ToString();

Peek 函数帮助读取并建议 MVC 为后续请求维护“TempData”。

If you need to store data for longer than a single redirect you should use Keep or Peek methods.

string data = TempData["id"].;
TempData.Keep("id");

or simply use,

string data = TempData.Peek("id").ToString();

Peek function helps to read as well as advice MVC to maintain “TempData” for the subsequent request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文