Reputation: 38228
I've tried to create a simple hello world asp.net mvc app with this requirement: It has an input box which is number. The user can change the number value, if ever the value is text instead of a number, the webpage should show the previous number entered.
The problem is I can't a way to do so as it seems that Controller and Model are stateless. Am I right? Does it mean for mocking even such simple case I have to setup a database?
Upvotes: 0
Views: 636
Reputation: 26
Of course you can use Sessions in MVC, just declare it in the attributes of the class (not only readonly, you can change that):
namespace NamespaceName {
[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class ClassName : Controller {
public ActionResult MethodName(string param) {
// CODE HERE
}
}
}
Then you'll have access to HttpContext.Session.
Upvotes: 1
Reputation: 48279
Controller and model are stateless and you could possibly work this around by involving session or any other server-side persistence mechanism but why bother?
Instead, make the client stateful - add some javascript code that validates the input and restores the previous value if the validation fails. Then, only upon succesfull client-side validation, post the form to the server.
Upvotes: 2