What is the scope of ViewData Dictionary?I mean when It Creates for a View & when it destroys?
Lifecycle of ViewDataDictionary.
Since ViewData is a dictionary, it contains key-value pairs where each key must be a string. ViewData only transfers data from controller to view, not vice-versa.
As you can see, the ViewDataDictionary class implements the IDictionary interface. So we can say that the ViewData in ASP.NET MVC Framework is a dictionary object.
ViewData is a dictionary of objects that are stored and retrieved using strings as keys. It is used to transfer data from Controller to View. Since ViewData is a dictionary, it contains key-value pairs where each key must be a string.
Csharp Server Side Programming Programming ViewData is a dictionary of objects that are stored and retrieved using strings as keys. It is used to transfer data from Controller to View. Since ViewData is a dictionary, it contains key-value pairs where each key must be a string.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With