鉴于此,是否存在TempData可靠使用的情况?
或者以任何方式删除或缓解用户刷新的问题?
解决方法
但是,对于MVC 2,临时数据在首次尝试访问它后会丢失.
您始终可以使用TempData使用的Session来解决您遇到的临时数据丢失问题.
来自MVC 2 Beta发行说明:
TempDataDictionary Improvements
The behavior of the TempDataDictionary
class has been changed slightly to
address scenarios where temp data was
either removed prematurely or
persisted longer than necessary. For
example,in cases where temp data was
read in the same request in which it
was set,the temp data was persisting
for the next request even though the
intent was to remove it. In other
cases,temp data was not persisted
across multiple consecutive redirects.To address these scenarios,the
TempDataDictionary class was changed
so that all the keys survive
indefinitely until the key is read
from the TempDataDictionary object.
The Keep method was added to
TempDataDictionary to let you indicate
that the value should not be removed
after reading. The
RedirectToActionResult is an example
where the Keep method is called in
order to retain all the keys for the
next request.
您还可以直接查看MVC 2源以查看这些更改:
MVC 1:
public object this[string key] { get { object value; if (TryGetValue(key,out value)) { return value; } return null; } set { _data[key] = value; _modifiedKeys.Add(key); } }
MVC 2:
public object this[string key] { get { object value; if (TryGetValue(key,out value)) { _initialKeys.Remove(key); return value; } return null; } set { _data[key] = value; _initialKeys.Add(key); } }