ASP.Net MVC TempData - 如何保持状态

发布于 2024-09-25 08:48:32 字数 196 浏览 0 评论 0原文

我们使用 ASP.Net MVC TempData 来存储页面刷新之间的表单数据。我们在页面上有一个按钮,允许用户执行特定操作。如果用户单击此按钮一次,它就可以正常工作。如果他们单击按钮两次(这是允许的),我们就会丢失 TempData 数据。我们需要确保无论用户单击按钮多少次,TempData 数据都会被保留。顺便说一句,该按钮激活 URL.Action,并利用 Ajax。

We are using ASP.Net MVC TempData to store form data between page refreshes. We have a button on the page that allows the user to perform a certain action. If the user clicks this button one time, it works fine. If they click the button twice, which is allowed, we lose the TempData data. We need to make sure the TempData data is preserved no matter how many times the user clicks the button. By the way, the button activates a URL.Action, and utilizes Ajax.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

友谊不毕业 2024-10-02 08:48:32

TempData.Keep()?

TempData.Keep()?

水晶透心 2024-10-02 08:48:32

我建议将数据放在 Session 中,而不是放在 TempData 中,因为 TempData 只存储直到下一个请求结束时的数据。在您的情况下发生的情况是,用户每次单击按钮时都会发出请求,因此在第二次单击按钮时,TempData 已被清除(或将在第一个请求结束时被清除)。

您可以在第一次单击按钮后禁用该按钮,但这可能会导致解决方案不太稳健。使用Session和另一个AJAX请求来清除第一个AJAX请求成功返回时所做的Session中的数据将确保您知道第一个AJAX请求返回并且可以清除Session中的数据

I'd suggest putting the data in Session as opposed to TempData as TempData only stores data up to the end of the next Request. What is happening in your situation is that the user is making a request each time they click the button, so on the second button click, TempData has already been cleared (or will be cleared at the end of the first request).

You could disable the button after it is clicked the first time, but this may lead to a less robust solution. Using Session and another AJAX request to clear the data in Session made when the first AJAX request returns successfully will ensure that you know that the first AJAX request returned and the data in Session can be cleared

番薯 2024-10-02 08:48:32

我们需要确保无论用户单击按钮多少次,TempData 数据都会被保留

TempData 按钮将数据传递到下一个操作/请求多少次,都会保留 TempData 数据。这对于 Session 来说更是如此。

如果您仍想使用 TempData,请确保所有相关控制器操作再次设置相同的 TempData 值。

We need to make sure the TempData data is preserved no matter how many times the user clicks the button

TempData is to pass data to the very next action/request. That's more a case for Session.

If you still want to go for TempData, make sure all the related controller actions set again the same TempData value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文