ASP.NET TempData的请求之间仍然存在 [英] ASP.NET TempData persists between requests

查看:220
本文介绍了ASP.NET TempData的请求之间仍然存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的临时数据,按照我的控制器 - 很简单的,当有一个问题:

I am using temp data as follow in my controllers - very simple, when there is a problem:

TempData("StatusMessage") = "You have no items set to Auto-Ship."

然后在每一页我有一个用户控件,如下所示的:

Then on every page I have a user control as follows:

<div class="error-container">
<%  If TempData.ContainsKey("ErrorMessage") Then%>
<script> $('div.error-container').show();</script>
<div class="msg-error"><p><%=TempData("ErrorMessage") %></p></div>
<% End If%>
<%  If TempData.ContainsKey("StatusMessage") Then%>
<script> $('div.error-container').show();</script>
<div class="msg-status"><p><%=TempData("StatusMessage")%></p></div>
<% End If%>
<ul></ul>
</div>

问题是,当我有添加了一个错误的TempData它能够正确显示的第一个要求,也是对下一个请求再次显示为良好 - 这显然是非常混乱,而不是一个期望的行为

Problem is when I do have an error added to tempdata it shows up properly on the first request but ALSO shows up again on the next request as well - which is obviously very confusing and not a desired behavior.

我没有使用任何的IoC,我没有看到后使用,当同样的问题。

I am not using any IoC, I did see the post with the same problems when using that.

推荐答案

的唯一目的的TempData的是要持续到下一个请求。东西你不想持续到下一个请求应该进入的ViewData代替。

The sole purpose of TempData is to persist until the next request. Stuff you do not want to persist until the next request should go into ViewData, instead.

实际上,这意味着TempData的只是安全的重定向时使用。当不重定向时,下一个请求可以是任何东西。

Realistically, this means that TempData is only safe to use when redirecting. When not redirecting, the "next request" could be anything.

这篇关于ASP.NET TempData的请求之间仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆