Reputation: 10415
I'm going to ftech list of my application admins(users with role 'admin') and store them in a List<ApplicationUser> AdminList
inside a controller of a custom model and in its create
action. AdminList
is populated inside create action to populate a drop-down in create
view.
I want to know is it possible that the list i.e. AdminList
will be disposed among calling create and its postback? In other words, is it required to populate AdminList
again inside postback method or dispose will never happen?
Upvotes: 0
Views: 101
Reputation: 219037
HTTP is stateless.
Unlike things like WPF applications or Winfows Forms applications, web applications don't maintain a "running application" filled with in-memory state. (At least not in the same intuitive way.) Each request builds up a new state each time. This includes, in the case of MVC, a new instance of the Controller. (Or in the case of Web Forms, a new instance of the Page.)
In order for the data to be persisted from one request to another, you'd need to persist it somewhere. "Somewhere" could be a whole host of different places. The page's form elements, cookies, session, static variables, a database, a file, etc.
For example, if this "admin list" is relatively static and unlikely to change, and is the same for all users of the site, then you might store it in a static property which gets lazy-loaded if it's not set. Something structurally like this:
private static List<ApplicationUser> _adminList;
private static List<ApplicationUser> AdminList
{
get
{
if (_adminList == null)
_adminList = GetAdminsFromData();
return _adminList;
}
}
(However you populate the list would be what that function call does, of course.)
That way the consuming code never really needs to know or care if the list has been populated or not. Just consume the AdminList
property and, if it hasn't been populated (if for whatever reason the static
context has been cleared, such as an application re-start) then it'll be populated. Otherwise it'll just contain whatever was last put there.
Upvotes: 1