Let's keep this simple! :-)
You asked following questions:
I want to maintain model state between method calls on the controller.
Is that possible?
Answer: yes, it is
A controller is created at the call of some of its methods and destroyed when the called method finishes its execution?
Answer: yes, it doesn't persist any state - it just returns the result generated by the action
If not... how can I maintain the state between method calls? If that is possible... how has it to be written?
Answer:
We had the same issue in our current business application using ASP.NET MVC2 and came up with following solutions:
Solution(s):
1st he Proof-Of-Concept for this application contained some special functionality using the global Session. (Note that the session is saved on the webserver)
Saving and reading from session in ASP.NET MVC:
public Action SaveIntoSession()
{
...
Session["SessionData"] = "Something to be stored in session";
...
}
public action ReadFromSession()
{
...
// UNBOXING is required when you're using the session as ASP.NET doesn't know what is stored into the session
string sessionData = (string)Session["SessionData"];
...
}
2nd There's another concept of saving the required information for each request into a Session Table into your database. (But I won't go too deep into that.. check this article for more info - although it's php, you can get your head around the concept pretty easily)
3rd Would be to use the TempDataDictionary as mentioned by Charlino. The problem with this is, that it only persists the state from one call to the another. All data that is stored will be deleted by the next request. (TempData is mostly used to display errors to the user..)
public Action SaveIntoTempData()
{
...
TempData["TempData"] = "Something to be stored ONE request ONLY";
...
}
public Action ReadFromTempData()
{
...
string tempData = (string)TempData["TempData"];
}
4th You could also use the ViewDataDictionary. It is not recommended to be used with sensitive data. (check this thread for more info)
public Action SaveIntoViewData()
{
...
TempData["ViewData"] = "Something to be stored into the page";
...
}
public Action ReadFromViewData()
{
...
string viewData = (string)ViewData["ViewData"];
}
At the end of the day. It is up to you and your team what best matches requirements.