ASP.Net MVC TempData - how to keep state
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 Aj开发者_StackOverflowax.
TempData.Keep()?
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
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.
精彩评论