In the first example, your session variable points to a link, so it is updated as two links point to the same value.
so before you assign a session, you convert it to Json and then assign
HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);
Note. JsonConvert from Newtonsoft.Json namespace namespace in C #
In the second line, if the value changes in the listModel object, which does not reflect the session. but when you want to get a value from a session, you have to convert it to a Json object form
if (HttpContext.Current.Session[listModelType + "ListModel"] != null) { listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]); }
source share