xr280xr
xr280xr

Reputation: 13302

Correct way to "embrace" statelessness of asp.net MVC

I'm working on updating a mobile website that was built using ASP.NET MVC2 to mimic some functionality of a non-mobile website that was built using traditional ASP.NET forms. This is my first time working in MVC. I've read several before me asking about how to overcome the loss of ViewState, and I get the idea of MVC and why it is stateless, but I'm not sure exactly how to manage instances that required statefulness in the original site.

For instance, I have a view that requires a web service to be called to load a collection of payment plans. The payment plans are loaded and displayed to the user. When the user selects a payment plan, the form will post and I need to do some calculations based on the properties of the selected payment plan and possibly properties of the other payment plans. But, of course, they no longer exist. Options and problems as I see it:

I don't, at all, love ViewState or traditional ASP.NET and so far really like MVC but am just not yet sure how to handle this type of thing. Thanks in advance.

To try avoid my question being construed as being to broad, the question is: When state is necessary, as it seems to be here, how do you store it with MVC2?

Upvotes: 1

Views: 586

Answers (1)

John Saunders
John Saunders

Reputation: 161821

I don't see a problem with using Session state or something more global like Application state or Cache. All you're doing is caching a value from the service. This is "optional state". Your code will still work even if you don't find the values you were saving in Cache.

When they say "stateless", this isn't what they mean. They mean that, for instance, the behavior of your controller methods should not depend on previous calls to the controller method. Everything the controller needs to operate should be passed to it on every request, perhaps in ViewBag.

BTW, the web was "stateless" before ASP.NET MVC. It's just that web forms allow you to write code that behaves as though the controls had state, when they really did not.

Upvotes: 1

Related Questions