views:

71

answers:

4

Hi...

I want to maintain model state between method calls on the controller.
Is that possible?

A controller is created at the call of some of its methods and destroyed when the called method finishes its execution?

If not... how can I maintain the state between method calls? If that is possible... how has it to be written?

thanks

A: 

I think by default you should be trying for a Restfull approach. If that is not viable then either serialise to the session object or store in a Singleton or something like that.

By default I don't think MVC maintains state between calls.

griegs
+1  A: 

Would TempData / TempDataDictionary be suitable?

HTHs,
Charles

Charlino
TempData will only survive a redirect so it won't work in this case.
Ryan
The question was for a solution to *maintain the state between method calls* - I think TempData acomplishes this. Yes? Sure there are other ways, but it's a bit blind to say that it won't work in this case given the ambiguity or the question.
Charlino
Sorry I thought "between method calls" meant separate calls from the browser.
Ryan
+2  A: 

You have access to the standard ASP.NET session, even in MVC. Check the docs.

Connor M
+1  A: 

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.

Shaharyar