Saving all members of a model class in a session

In a newly created MVC4 application, I would like to keep all USERProfile members in the session so that I can access all the values ​​after the user logs in. But the session array object offers only 2 alternative

Session[int]/Session[string]

I need to get

Session['username']; Session['age']; etc

everything existing in this class.

+6
source share
2 answers

You can save

 Session["UserProfile"]=UserProfile; // User Profile being an object 

A caveat is when you retrieve your profile that you must use it:

 UserProfile profile = (UserProfile) Session["UserProfile"] 
+9
source

What you specified are keys that you can use to access something in the session. The key can be either int or string.

Just do

 Session.Add("MyProfile", USERProfileObject); 

and you can save this entire object in the session.

Please note that: http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.add.aspx

indicates that it takes a string and an object.

0
source

All Articles