I have an ASP.NET MVC page, and inside the page I will make a few AJAX call.
What is the property inside the Controller whose values are persisted as long as the page is still there ( even during AJAX call), and are destroyed when I switch to other page request?
Controller.Session‘s keys and values, as we all know it, are alive and accessible all the time. Controller.Request are alive when the page is constructed or when an AJAX call is made. Request variables don’t persist from a normal page call to subsequent AJAX call at the same page.
Any ideas?
A page is never truly “there”. In ASP.NET MVC there is no concept of pages at all. Pages in the sense of interface entities with the lifespan stretching over several requests with the ability to persist some data and the controls state. There are views which only exist for a single moment with no ability to persist any data.
There are requests. You requested a page you’ve got it. Server has returned it to you and immediately forgotten about it. Your Ajax call is a completely new request. The server won’t even know if it’s an Ajax request. For it, all requests are equal, Get, Post or otherwise.
You can use the TempData collection, it will persists the data between two consequent requests.
The other alternative is the Session collection. It will keep data between many request of your session.