c# Mvc 3 Tempdata Dictionary?

In my mvc3 project, I keep Dictionary in Tempdata Dictionary.

TempData[ViewDataKeys.ItemsAdd] = new Dictionary<int, int>();

When Ajax request i add items to TempData[ViewDataKeys.ItemsAdd]. It is updated but when i remove items from TempData[ViewDataKeys.ItemsAdd] it is not updated.

after remove one items from TempData[ViewDataKeys.ItemsAdd] , it is null.

 private void RemoveFrom(int Uid)
    {
        if (TempData[ViewDataKeys.ItemsAdd] == null)
        {
            TempData[ViewDataKeys.ItemsAdd] = new Dictionary<int, int>();
        }
        var a= (Dictionary<int,int>)TempData[ViewDataKeys.ItemsAdd];
        a.Remove(Uid);
        TempData[ViewDataKeys.ItemsAdd] = a;
    }


        private void AddTo(int Uid)
    {
        if (TempData[ViewDataKeys.ItemsAdd] == null)
        {
            TempData[ViewDataKeys.ItemsAdd] = new Dictionary<int,int>();
        }

        var a= (Dictionary<int,int>)TempData[ViewDataKeys.ItemsAdd];
        a.Add(Uid, Uid);
        TempData[ViewDataKeys.ItemsAdd] = a;
    }

Answers


Whenever TempData is read the value it contains is returned and then it's removed from TempData. TempData actually uses the standard Session object but the difference is when you read a value the TempData object is removed from the session.

Generally it's only to be used for redirects or something you need to persist to the next page load.


Need Your Help

Development environment sandboxing

c++ c virtual-machine lxc linux-containers

What is the standard way to isolate development environment from system when developing in Linux? For example when I work on applications or packages which the system already use. I know could I us...

Hosting Microsoft.ServiceModel.WebSockets.WebSocketService

wcf websocket iis-8 self-hosting wcf-hosting

I was reading this article about WCF Websockets, and there is a nice example of a WebSocket service using Microsoft.ServiceModel.WebSockets.WebSocketService, that looks perfect for a thing I want t...