If I redirect to a new page passing TempData to initialise the page it works fine, however if the user presses the refresh button in their browser the TempData is no-longer available.
Given this, is there any situation where TempData could be used reliably?
Or any way to remove or mitigate the problem of users refreshing?
相关问题
- MVC-Routing,Why i can not ignore defaults,The matc
- parameters in routing do not work MVC 3
- There is no ViewData item with the key 'taskTy
- TextBoxFor decimal
- Install ASP.NET 5.0 version of System.ServiceModel
相关文章
- How to get a list of connected clients on SignalR
- How do you redirect to the calling page in ASP.NET
- Change color of bars depending on value in Highcha
- The program '[4432] iisexpress.exe' has ex
- ASP.Net MVC 4 Bundles
- How to get server path of physical path ?
- Cannot implicitly convert Web.Http.Results.JsonRes
- entity type has no key defined - Code first
A workaround for the the given situation in MVC1 would be to re-assign the TempData in the second controller as well. Of course it persists the data in the system for a bit more time. but it fixes the refresh issue.
Tempdata is used across redirects so if you are refreshing the page that means you are making a separate request to the server so that is why your data gets lost. To persist this data call Tempdata.Keep("KeyofTempdata") method in the action to which you are redirecting. If you want to remove data use Tempdata.Remove("KeyofTempdata").
In MVC 1, yes, temp data is lost after the next request after you store a key.
With MVC 2 however, the temp data is lost after the first attempt to access it.
You can always use Session, which TempData uses anyway, to solve the temp data loss issue your having.
From the MVC 2 Beta Release Notes:
You can also look directly in the MVC 2 source to see these changes:
MVC 1:
MVC 2:
The only features that can solve your issue is
Cache
andSession
.ViewData
essentially 'dies' out when the view is generated.If you can provide more details on what you're trying to accomplish, maybe another solution can be given, however, it seems the best option for you is to use
Session
orCache
.TempData exists specifically to store the data for just one page load/action/redirect. If you need the data to persist after a refresh you should place it in the ViewData collection so long as the action that is serving the refresh request is the same one as was initially requested (i.e. the ViewData value was not added prior to a Redirect).
You should write
in your controller, then it will keep that data in refresh situations too.