TempData的是不清除预期 [英] TempData is not clearing as expected

查看:177
本文介绍了TempData的是不清除预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET 4.0和2.0 MVC应用程序。如果它以任何方式相关的,我使用VS2010。

I'm working on an application using ASP.NET 4.0 and MVC 2.0. If it's in any way relevant, I'm using VS2010.

我正在与TempData的并发症。我没有写原来的code,但它不能正常工作,我试图修复它。我没有很多的经验与TempData的和ViewData的工作。

I'm running into complications with TempData. I did not write the original code, but it isn't working correctly and I'm attempting to fix it. I don't have a lot of experience working with TempData and ViewData.

我有一个指数操作如下(伪code):

I have an Index action as follows (pseudocode):

public virtual ActionResult Index()
{
   var vm = new IndexViewModel();

   // some code here to set up the ViewModel

   if (TempData.ContainsKey("Success"))
      vm.Success = true; 

   return View(MVC.Controller.Views.Index, vm);
}

和我有一个POST操作如下(伪code):

And I have a POST action as follows (pseudocode):

[HttpPost]
public virtual ActionResult Index(IndexViewModel vm, List<int> formData)
{
    if (DoSomethingWithData(formData))
    {
       TempData["Success"] = true;
       return RedirectToAction(MVC.Controller.ActionNames.Index);
    }

    TempData["Message"] = "Failed to use formData";

    return View(MVC.Controller.Views.Index, vm);
}

该视图发出的形式和$ P $一个成功的消息pfaces,如果vm.Success是真实的。它也将发出在TempData的消息[消息]如果是present。

The view emits a form and prefaces it with a success message if vm.Success is true. It will also emit the message in TempData["Message"] if it is present.

我第一次来到页I得到的只是形式。我输入无效表单数据,并提交......,我得到了错误信息pfaced形式$ P $预期。 (我知道有不良设计在这里,因为它不重定向...和你得到刷新,等用户体验不佳,但我并不担心没有)这是所有伟大的。

The first time I come to the page I get just the form. I enter INVALID form data and submit it... and I get the form prefaced by the error message as expected. (I know there's poor design here since it doesn't redirect... and you get poor user experience with refresh, etc. but I'm not worried about that yet) This is all great.

问题清单当我使用有效的表单数据。如果我提交有效表单数据,我得到一个成功的消息pfaced如预期的页面返回$ P $,但如果我刷新页面成功消息依然存在。事实上,如果我去的网站完全不同的部分,然后返回,成功的消息依然存在。对于重定向和阅读后的一些原因,TempData的仍然存在。已经有两个重定向和读......现在应该不是临时数据清楚?

The problem manifests when I use VALID form data. If I submit valid form data, I get the page back prefaced with a success message as expected, but if I refresh the page the success message is still there. Indeed if I go to a completely different part of the site and navigate back, the success message is still there. For some reason after a redirect and read, the tempdata is still there. There has been both a redirect and a read... shouldn't the temp data now be clear?

我有理由相信,其他地方我浏览到未设置的TempData [成功]因任何原因,但可以肯定我已经浏览到的东西​​像谷歌,回来直接到网址为这个页面,它仍然好像TempData的[成功]被填充。

I'm reasonably certain that the other places I navigate to aren't setting TempData["Success"] for any reason, but to be sure I've navigated to things like Google, and come back directly to the URL for this page, and it still seems as though TempData["Success"] is populated.

这是非常清楚的,要么我没有清楚地了解TempData的应该是怎样的功能(不足为奇),或者不同寻常的事情,我根本就没有经验去看看。

It's very clear that either I don't clearly understand how TempData is supposed to function (unsurprising) or something unusual is happening that I simply don't have the experience to see.

任何意见,欢迎!

谢谢,
戴夫

实际上并不发射形式时,有一个成功消息的观点......它只是发射成功的消息。

The view doesn't actually emit the form when there's a success message... it only emits the success message.

视图看上去或多或少是这样的:

The view looks more or less like this:

<% if (TempData.ContainsKey("Message")) { %>
   Emit message...
<% } %>

<% using (Html.BeginForm(MVC.Controller.ActionNames.Index, 
                         MVC.Controller.Name, 
                         FormMethod.Post, 
                         new { id = "form"})) { %>
   <% if (!Model.Success) { %>
      Emit form...
   <% } else { %>
      Emit confirmation message...
   <% } %>
<% } %>

旧金山指着我对一些我没有考虑过......但事实证明,在视图模型构造成功设置为false ...所以它不是奇怪这一点。我知道肯定的TempData [成功]仍设置(而不是像傻傻的重用成功设置为true的视图模型),因为我已经通过了code加大,并不断地步入了if语句的地方套vm.success = TRUE,即使在刷新后。

Francisco pointed me towards something I hadn't considered... but it turns out the constructor for the viewmodel sets Success to false... so it's not something odd with that. I know for sure that TempData["Success"] is still set (rather than something like foolishly reusing a viewmodel with success set to true) because I've stepped through the code and it continually steps into that if statement where it sets vm.success = true, even after a refresh.

推荐答案

只需添加这正如我在我的评论说。我建议做

Just adding this as I said in my comment. I suggest to do

if (TempData["Success"] != null) 
    vm.Success = true;

而不是

if (TempData.ContainsKey("Success"))
      vm.Success = true; 

...所以它算作一个TempData的阅读。很高兴它的工作。
问候

... so it counts as a TempData read. Glad it worked. Regards

这篇关于TempData的是不清除预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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