Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中更新对象时,会话对象会发生更改#_C#_Asp.net_.net_Session_Session Variables - Fatal编程技术网

C# 在C中更新对象时,会话对象会发生更改#

C# 在C中更新对象时,会话对象会发生更改#,c#,asp.net,.net,session,session-variables,C#,Asp.net,.net,Session,Session Variables,我有一个非常奇怪的问题,我肯定我错过了一些明显的东西。我有以下两行: HttpContext.Current.Session[listModelType + "ListModel"] = listModel; listModel.ProductRows = new Collection<ProductRow>(listModel.ProductRows.Where(r => r.ParentRowId == 0).ToList()); 而HttpContext.Current

我有一个非常奇怪的问题,我肯定我错过了一些明显的东西。我有以下两行:

HttpContext.Current.Session[listModelType + "ListModel"] = listModel;
listModel.ProductRows = new Collection<ProductRow>(listModel.ProductRows.Where(r => r.ParentRowId == 0).ToList());
而HttpContext.Current.Session[“i”]保持为0。

请参阅

int
是一种值类型,因此在赋值时将按“原样”存储;您的
listModel
是引用类型,因此您在会话中存储对对象的引用,而不是对象的值


如果希望会话中的一个实例保持不变,则必须创建一个新的
listModel
实例。

在第一个示例中,您存储了对对象(列表内存位置)的引用。因此,如果更新了
列表
,它将反映在会话中。这是一种引用类型

在第二个示例中,您使用的是值类型:

int i = 0;
HttpContext.Current.Session["i"] = i;
i++;
声明
i
并将其设置为0(值类型)

在会话中存储值
0
。(不是
i
的内存位置)


您增加了
i
,但会话仍然具有值
0

。在第一个示例中,会话变量指向一个
引用
,因此它会被更新,因为两个引用指向相同的值


第二个会话变量指向一个
原语(值)
类型,因此它们有单独的值副本。

正确的方法应该是:

  int i = 0;
  i++;
  HttpContext.Current.Session["i"] = i;

HttpContext.Current.Session[“i”]保持为1。

在第一个示例中,会话变量指向一个引用,因此它会被更新,因为这两个引用指向相同的值

因此,在分配给会话之前,将其转换为Json,然后分配

HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);
   HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);
注意:JsonConvert来自c#中的名称空间Newtonsoft.Json名称空间

在第二行中,如果listModel对象中的值更改,则该对象不反映会话。 但是,当您想从会话中检索值时,应该将其转换为对象形式的Json

if (HttpContext.Current.Session[listModelType + "ListModel"] != null)
{

    listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]);

}
   if (HttpContext.Current.Session[listModelType + "ListModel"] != null) {

        listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]);

    }
if(HttpContext.Current.Session[listModelType+“ListModel”]!=null)
{
listModel=JsonConvert.DeserializeObject((字符串)HttpContext.Current.Session[listModelType+“listModel]”);
}

在第一个示例中,会话变量指向一个引用,因此它会被更新,因为这两个引用指向相同的值

因此,在分配给会话之前,将其转换为Json,然后分配

HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);
   HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);
注意:JsonConvert来自c中的名称空间Newtonsoft.Json命名空间#

在第二行中,如果listModel对象中的值更改,则该对象不反映会话。但是,当您想从会话中检索值时,应该将其转换为对象形式的Json

if (HttpContext.Current.Session[listModelType + "ListModel"] != null)
{

    listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]);

}
   if (HttpContext.Current.Session[listModelType + "ListModel"] != null) {

        listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]);

    }
if(HttpContext.Current.Session[listModelType+“ListModel”]!=null){
listModel=JsonConvert.DeserializeObject((字符串)HttpContext.Current.Session[listModelType+“listModel]”);
}

谢谢CodeCaster-我们将试一试。你的权利根据你的想法,价值存储在会话中