SAHIL SINGLA
SAHIL SINGLA

Reputation: 755

What is the scope of ViewData Dictionary?

What is the scope of ViewData Dictionary?I mean when It Creates for a View & when it destroys?

Lifecycle of ViewDataDictionary.

Upvotes: 6

Views: 2506

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039298

The ViewData dictionary is created by the controller (more precisely the first time you access it) is released after the view finished rendering. Excerpt from the getter:

public ViewDataDictionary ViewData
{
    get
    {
        if (this._viewDataDictionary == null)
        {
            this._viewDataDictionary = new ViewDataDictionary();
        }
        return this._viewDataDictionary;
    }
    set
    {
        this._viewDataDictionary = value;
    }
}

Basically you may assume that the ViewData will be accessible from the beginning of the request inside you controller through the rendering of the view itself and it will be released after the page has finished rendering.

Upvotes: 7

Related Questions