asp.net-mvc-3 - 为什么 TempData[] 不适用于 IE

标签 asp.net-mvc-3 internet-explorer google-chrome razor tempdata

在我的 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();

    }
}

`

最佳答案

您永远不应该从将某些内容存储到 TempData 的 Controller 操作中返回 View 。您应该立即重定向到应该使用它的 Controller 操作:

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 仅在单个附加请求中存活。因此,例如,如果在 View 内部您向某个 Controller 操作(无论哪个)发送 AJAX 请求,然后在此 View 中有一个指向目标操作的链接,当用户重定向到此目标操作时,TempData 将不再存在因为它在之前完成的 AJAX 请求期间丢失了。

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

关于asp.net-mvc-3 - 为什么 TempData[] 不适用于 IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7901980/

相关文章:

javascript - Google Chrome 忽略 Cache-Control header 的原因

python 3.6 selenium webdriver 错误 X display is required for sending-keys unable to ues Xvfb

asp.net-mvc - 自定义工具 'RazorGenerator'失败。该方法或操作未实现

jquery - 在 asp.net MVC3 EF4.1 中创建父级时创建动态子级列表

css - 需要在表格上覆盖标题

css - 100% 宽度绝对定位 flexbox 不受 IE 中 CSS Grid 的约束

c# - MVC 3 : how to update checkboxlist when dropdownlistitem is selected?

asp.net-mvc-3 - MVC3 不显眼的验证在 IE 中不起作用

html - IE设置背景颜色时出现不必要的边框

html - 圆 Angular 包含在小分区中的大图像的侧面在 Chrome 中不起作用