ASP.NET MVC does browser refresh make TempData useless?

半城伤御伤魂 提交于 2019-12-03 01:32:46

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:

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.

You can also look directly in the MVC 2 source to see these changes:

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);
        }
    }
cemsazara

You should write

TempData.Keep("nameofthedata");

in your controller, then it will keep that data in refresh situations too.

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").

The only features that can solve your issue is Cache and Session.

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 or Cache.

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!