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.
我们使用ASP.Net MVC TempData在页面刷新之间存储表单数据。我们在页面上有一个按钮,允许用户执行某个操作。如果用户单击此按钮一次,则可以正常工作。如果他们单击按钮两次(允许),我们将丢失TempData数据。无论用户单击按钮多少次,我们都需要确保保留TempData数据。顺便说一句,该按钮激活URL.Action,并使用Ajax。
3 个解决方案
#1
2
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).
我建议将数据放在Session而不是TempData,因为TempData只存储数据直到下一个Request的结尾。在您的情况下发生的情况是,用户每次单击按钮时都会发出请求,因此在第二个按钮单击时,TempData已被清除(或将在第一个请求结束时清除)。
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
您可以在第一次单击按钮后禁用该按钮,但这可能会导致解决方案不太稳健。使用Session和另一个AJAX请求清除第一个AJAX请求成功返回时Session中的数据将确保您知道返回的第一个AJAX请求并且可以清除Session中的数据
#2
5
TempData.Keep()?
#3
0
We need to make sure the TempData data is preserved no matter how many times the user clicks the button
无论用户单击按钮多少次,我们都需要确保保留TempData数据
TempData is to pass data to the very next action/request. That's more a case for Session.
TempData将数据传递给下一个操作/请求。这更像是Session的一个案例。
If you still want to go for TempData, make sure all the related controller actions set again the same TempData value.
如果您仍想使用TempData,请确保所有相关的控制器操作再次设置相同的TempData值。
#1
2
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).
我建议将数据放在Session而不是TempData,因为TempData只存储数据直到下一个Request的结尾。在您的情况下发生的情况是,用户每次单击按钮时都会发出请求,因此在第二个按钮单击时,TempData已被清除(或将在第一个请求结束时清除)。
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
您可以在第一次单击按钮后禁用该按钮,但这可能会导致解决方案不太稳健。使用Session和另一个AJAX请求清除第一个AJAX请求成功返回时Session中的数据将确保您知道返回的第一个AJAX请求并且可以清除Session中的数据
#2
5
TempData.Keep()?
#3
0
We need to make sure the TempData data is preserved no matter how many times the user clicks the button
无论用户单击按钮多少次,我们都需要确保保留TempData数据
TempData is to pass data to the very next action/request. That's more a case for Session.
TempData将数据传递给下一个操作/请求。这更像是Session的一个案例。
If you still want to go for TempData, make sure all the related controller actions set again the same TempData value.
如果您仍想使用TempData,请确保所有相关的控制器操作再次设置相同的TempData值。