ASP.Net MVC TempData - 如何保持状态
我们使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TempData.Keep()?
TempData.Keep()?
我建议将数据放在 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
TempData 按钮将数据传递到下一个操作/请求多少次,都会保留 TempData 数据。这对于 Session 来说更是如此。
如果您仍想使用 TempData,请确保所有相关控制器操作再次设置相同的 TempData 值。
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.