0

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

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.

  • Is it a part od ViewData? – John Feb 25 '15 at 12:43
  • No. ViewData is simply a dictionary where keys are strings and values are objects, and doesn't contain your model. – Arseni Mourzenko Feb 25 '15 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 Feb 25 '15 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 Feb 25 '15 at 14:05
  • There is an answer here which also shows that I was wrong. – Arseni Mourzenko Feb 25 '15 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.