TempData[“message”] 不可靠——我做错了什么?
当用户在我的网站上执行如下操作时,我使用 TempDate["Message"] 显示小更新横幅:
[AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Admins")]
public ActionResult Delete(int id)
{
_Repo.DeletePage(id); // soft-delete
TempData["Message"] = "Page deleted!";
return RedirectToAction("Revisions", "Page", new { id = id });
}
然后在我的母版页中我有这样的:
<%-- message box (show it only if it contains a message) --%>
<% string Message = (TempData["Message"] ?? ViewData["Message"]) as string;
if(!string.IsNullOrEmpty(Message)){
%>
<div id="message"><%:Message %></div>
<% }
TempData["Message"] = null; ViewData["Message"] = null; %>
我同时点击 TempData 和 ViewData 因为我在某处读到 TempData 应该用于重定向,而 ViewData 应该用于其他情况。
问题是:消息通常不会立即显示。有时,需要在网站的不同部分单击一两次才能显示该消息。这很奇怪。
有什么想法吗?
I'm using TempDate["Message"]
to show little update banners as the user does things on my site like this:
[AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Admins")]
public ActionResult Delete(int id)
{
_Repo.DeletePage(id); // soft-delete
TempData["Message"] = "Page deleted!";
return RedirectToAction("Revisions", "Page", new { id = id });
}
Then in my master page I have this:
<%-- message box (show it only if it contains a message) --%>
<% string Message = (TempData["Message"] ?? ViewData["Message"]) as string;
if(!string.IsNullOrEmpty(Message)){
%>
<div id="message"><%:Message %></div>
<% }
TempData["Message"] = null; ViewData["Message"] = null; %>
I hit both TempData and ViewData because I read somewhere that TempData should be used for redirects and ViewData should be used otherwise.
The issue is: often the message won't show up right away. Sometimes it takes a click or two to different parts of the site for the message to show up. It's very strange.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该验证代码中使用
TempData["Message"]
的所有位置。对应于 ASP.NET MVC 浏览器刷新是否会使 TempData 无用?< /a> 您只能读取TempData["Message"]
一次(另请参阅 http://forums.asp.net/p/1528070/3694325.aspx)。在第一次使用TempData["Message"]
期间,TempData["Message"]
将从内部TempDataDictionary
中删除。可能最好仅在
Page
控制器的Revisions
操作内部使用TempData["Message"]
,而不是在母版页内部或在视图内。You should verify all places where you use
TempData["Message"]
in your code. Corresponds to ASP.NET MVC does browser refresh make TempData useless? you can readTempData["Message"]
only once (see also http://forums.asp.net/p/1528070/3694325.aspx). During the first uage ofTempData["Message"]
, theTempData["Message"]
will be deleted from the internalTempDataDictionary
.Probably it would be better to use
TempData["Message"]
only inside ofRevisions
action of thePage
controller and not inside of master page or inside a View.TempData 并不是为了将数据传递给视图,因此名称为 ViewData 就是为了这个目的。事实上,我根本想不出在视图定义中使用 TempData 的原因...
TempData 的一种非常常见的用法是在执行重定向时在控制器操作之间传递信息(示例中的修订操作)例如,上面的代码可以使用您的
TempData["Message"]
变量)。这是编码 MVC 交互(Post-Redirect-Get)的 PRG 方法中的常见做法,因为在执行重定向到 Get 时,您经常需要从初始目标操作传递信息。下面的一个示例说明了这在 Get 中如何有用,我经常默认使用新的视图模型,除非已经从 TempData 中的重定向传递了一个视图模型:
还有一件事;我看到您明确清除了视图中的 TempData 和 ViewData 字典条目。您不需要这样做,因为无论如何,他们已经到了生命周期的尽头......
快乐编码!
TempData is not intended to pass data to views, hence the name ViewData for that purpose. In fact, I can't think of a reason to use TempData from within a view definition at all...
One very common usage of TempData is the passing of information between controller actions when you do a redirect (the Revisions action in your example above, for instance, would be able to make use of your
TempData["Message"]
variable).This is common practice in the PRG means of coding MVC interactions (Post-Redirect-Get) since you often need to pass information from the initial target action when doing the Redirect to the Get. An example of how this might be useful in a Get is below where I often just default to a new viewmodel UNLESS there is one already passed from a redirect in TempData:
One more thing; I see you explicitly clearing your TempData and ViewData dictionary entries in your view. You don't need to do that as by that point they are at the end of their life spans anyway...
Happy coding!
如果您在应该使用 ViewData 的地方使用 TempData,那么您的应用程序的行为就是您所期望的行为。
您需要仔细检查是否仅在控制器执行重定向时将状态反馈存储在 TempData 中。否则,您应该使用 ViewData。
Your app's behavior is the one you'd expect if you're using TempData where you should be using ViewData.
You want to double-check that you're storing your status feedbacks in TempData only when the controller does a re-direct. Otherwise, you should use ViewData.
这听起来像是您需要进行一些单元测试来确认您所看到的行为。尝试使用此示例作为起点编写一些内容:
http://weblogs.asp.net/leftslipper/archive/2008/04/13/mvc-unit-testing-controller-actions-that-use-tempdata.aspx
This smells like you need a couple of unit tests to confirm the behavior you're seeing. Try writing up a couple using this example as a starting point:
http://weblogs.asp.net/leftslipper/archive/2008/04/13/mvc-unit-testing-controller-actions-that-use-tempdata.aspx
如果您为应用程序配置了多个工作进程,但会话状态模式为“InProc”,则您不能使用默认的 TempData 实现,因为会话状态变得不可用。 (请参阅 ASP.NET 会话状态和多个工作进程< /a>)
您可以尝试使用 MvcFutures CookieTempDataProvider 代替。
If you have configured multiple worker process for your application, but session state mode is "InProc", then you can't use default TempData implementation, as session state becomes unusable. (see ASP.NET session state and multiple worker processes)
You could try to use MvcFutures CookieTempDataProvider instead.