Last post Sep 06, 2015 12:57 PM by sitby
Sep 06, 2015 11:02 AM|sitby|LINK
Is there something similar to Session.Add() that doesn't overwrite the var if it's already used?
and I need to just add an element to it each time instead of overwriting. Basically like a normal list would but that isn't lost after we leave action method. Is there something that does this?
Sep 06, 2015 12:31 PM|PatriceSc|LINK
Seems a confusion between "adding" a new session variable name with the corresponding value or adding a new entry to this particular value. For now my guess is that myList is a new list rather than taking its source from the existing UserSongs session variable?
So instead it would be something such as:
List<string> myList = (List<string>)Session["UserSongs"]; // Take the value from the current session variable
if (myList == null) // Not yet stored in session, create a new list and store it as a session variable
myList = new List<string>();
myList.Add(myList.Count().ToString()); // Add a new entry
I'm just adding a new entry which is the number of elements for demonstration purpose. You could expose thiis using a strongly typed class ie your code would be just something such as :
Behind the scene, this class ensure the session variable exists etc...
Sep 06, 2015 12:57 PM|sitby|LINK
Brilliant explanation. Thank you.