为什么 TempData[] 不适用于 IE
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您永远不应该从将某些内容存储到 TempData 的控制器操作返回视图。您应该立即重定向到应该使用它的控制器操作:
原因是 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:
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.
如果您需要存储数据的时间长于单个重定向,则应使用 Keep 或 Peek 方法。
或者简单地使用
Peek 函数帮助读取并建议 MVC 为后续请求维护“TempData”。
If you need to store data for longer than a single redirect you should use Keep or Peek methods.
or simply use,
Peek function helps to read as well as advice MVC to maintain “TempData” for the subsequent request.