ASP.NET MVC – Where Is Model Data Stored?

asp.net-mvcasp.net-mvc-4mvc

I am using ViewModel classes in order to structure data being populated inside of a controller.

My questions is now where exactly is data of a @model stored after being populated via asp mvc controller.

Best Answer

In MVC, the model (that is an instance of a model class) is nothing more than an ordinary object. It is initialized by the controller which passes it to MVC's engine which, in turn, uses it when generating the final result from a view.

If you're asking whether it is stored on the stack or on the heap, the response is: on the heap.

Instance variables for a reference type are always on the heap.

(Source; see also: What and where are the stack and heap?)

If you're asking whether it is stored in memory or on a hard disk, the response is: it depends. In general, it will be in memory, unless the operating system runs out of memory and decides to move it to the pagefile (chances for this to happen are slim).

If you're asking how should you access the model once initialized (i.e. where to find the instance of the model class), just keep a reference to it within the controller itself.