ASP.NET MVC, maintaining model state between Ajax requests

problem: The first time I request a full page, my controller calls the ApplicationServices Layer (a web service proxy for my business layer) to populate the current services collection, which is stored in my own property of the controller’s base class. This should then be displayed in the view. Everything in the context of this controller has access to this Service Collection. Now, when I make further calls to the same action method through AJAX Call, I obviously see that another instance of this controller means that my set of services is empty.

Otherwise, how to get the whole collection again, where will I keep this collection so that it stays between ajax requests? Should I save it as a separate DomainModel object, a Session object? .... since ViewData does not work for me obv. Sorry my ignorance MVC :)

Any help would be greatly appreciated :)

+4
source share
1 answer

The network is essentially stateless, and MVC helps you go down to the metal, i.e. MVC is not trying to do something inactive, which is not, and this is basically the path to the old ASP: each request is a request and it should not know anything about any other requests that have been executed in the past.

I feel it’s easiest to go this route because it tends to stay clean, fast and helps you to keep up with best practices, such as sharing problems.

AJAX takes the next step: the idea of ​​AJAX is that the simple “delete” operation can be implemented as such, i.e. you only need to authorize and execute one very small request at the save level. It. You do not even need to transfer the modified page to the user. A fairly simple machine-readable success / error indication via JSON.

If you start attracting many services for small AJAX requests, you really lose most of what is good for him.

I also suggest that you do not store a bunch of services in the base controller. Most likely, for most queries, you only need a small subset. It’s best to use only the services that you absolutely need.

+2
source

Source: https://habr.com/ru/post/1303449/


All Articles