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.
1 Answer 1
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.
-
-
No. ViewData is simply a dictionary where keys are
string
s and values areobject
s, and doesn't contain your model.Arseni Mourzenko– Arseni Mourzenko2015年02月25日 13:05:33 +00:00Commented Feb 25, 2015 at 13:05 -
how come I am able to access Model from razor using ViewData.Model syntax? If a model is a heap do I have to delete it manually or it will be deallocated automatically?John– John2015年02月25日 13:11:28 +00:00Commented Feb 25, 2015 at 13:11
-
Interesting. I'll check that and come back. As for your second question, in C#, objects are handled by the garbage collector. Unless you have serious reasons to do it, do not deallocate objects manually.Arseni Mourzenko– Arseni Mourzenko2015年02月25日 14:05:03 +00:00Commented Feb 25, 2015 at 14:05
-
There is an answer here which also shows that I was wrong.Arseni Mourzenko– Arseni Mourzenko2015年02月25日 15:22:51 +00:00Commented Feb 25, 2015 at 15:22