当 IList 的本地副本更改时,存储在 Session 中的 IList 也会更新
我正在构建一个大型的、多区域的 MVC 项目。 (MVC 2 测试版)。
我有一个会话包装类,在它自己的项目 MySession 中,如下所示:
Public Class MySession
Private Shared _userlist As String = "UserList"
Public Shared Property UserList() As IList
Get
If HttpContext.Current.Session(_userlist) Is Nothing Then
Return Nothing
Else
Return CType(HttpContext.Current.Session(_userlist), IList)
End If
End Get
Set(ByVal value As IList)
HttpContext.Current.Session(_userlist) = value
End Set
End Property
End Class
在子区域项目中,我通过包装器构建并保存一个列表到会话:
Dim favoritesList = (From m In db.getFavorites(UserId) Select m).ToList
MySession.UserList = favoritesList
这工作正常。我稍后将会话中的列表检索到本地变量 userList 中:
Dim userList As IList(Of getFavoritesResult)
userList = MySession.UserList
然后将一个新项目添加到本地 userList 中(并询问用户是否要保存它):
userList.Add(New getFavoritesResult With {.Id = addApp.AppId, .DisplayText = addApp.DisplayText, ...})
这是问题发生的地方。在上面的 .Add 方法之后,本地 userList 立即添加了新项目, 但是存储在会话中的 userList 还包含添加到本地列表中的新项目。
我不希望在会话中更新列表,除非用户想要保存新列表,并且我明确保存它。
为什么会发生这种情况?如何仅将项目添加到本地列表,而不是当前会话中的项目?
谢谢,这让我发疯!
I am building a large, multi-area MVC project. (MVC 2 Beta).
I have a session wrapper class, in its own project, MySession, that looks like this:
Public Class MySession
Private Shared _userlist As String = "UserList"
Public Shared Property UserList() As IList
Get
If HttpContext.Current.Session(_userlist) Is Nothing Then
Return Nothing
Else
Return CType(HttpContext.Current.Session(_userlist), IList)
End If
End Get
Set(ByVal value As IList)
HttpContext.Current.Session(_userlist) = value
End Set
End Property
End Class
In a child area project, I build and save a List to Session through my wrapper:
Dim favoritesList = (From m In db.getFavorites(UserId) Select m).ToList
MySession.UserList = favoritesList
This works fine. I later retrieve the list from session into a local variable, userList:
Dim userList As IList(Of getFavoritesResult)
userList = MySession.UserList
I then ADD a new item to the LOCAL userList (and will ask the user if they want to save it):
userList.Add(New getFavoritesResult With {.Id = addApp.AppId, .DisplayText = addApp.DisplayText, ...})
Here is where the problem occurs. Immediately after the above .Add method, the local userList has the new item added,
BUT the userList stored in Session ALSO contains the new item that was added to the local list.
I don't want the list updated in session unless the user wants to save the new list, and I explicitly save it.
Why does this occur, and how do I only add the item to the local list, not the one currently in session?
Thanks, this is driving me nuts!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在会话中存储的是对列表的引用。这就是您更新会话变量时更新的内容。如果您确实希望这些列表断开连接,则必须在将列表放入会话时创建列表的副本,或者在检索列表时创建列表的副本。我不知道 VB 语法,但类似于 (C#):
What you are storing in the session is a reference to a list. That is what you update when you update the session variable. If you really want these to be unconnected you will have to create a copy of the list when you put it in the session or create a copy when you retrieve it. I don't know the VB syntax but something like (C#):
发生这种情况是因为两个变量都引用同一个列表。
因此,当通过一个变量添加时,您将添加到同一个列表中。
您需要复制存储在
MySession
中的列表,对副本进行操作,并在准备好将MySession.UserList
保存到副本时设置。您可以通过以下方式轻松复制列表:
This is happening because both variables are referencing the same list.
So when adding via one variable, you are adding to the same list.
You need to copy the list stored in
MySession
, operate on the copy and when ready to save setMySession.UserList
to the copy.You can easily copy the list this way: