将对象从控制器传递到存储库时数据库更新错误的值
我有一个控制器 ActionResult,看起来像这样
Function Edit(ByVal user As Domain.User, ByVal id As Integer) As ActionResult
Dim _user As Domain.User = user
If ModelState.IsValid Then
If Not String.IsNullOrEmpty(user.UserName) AndAlso _
Not String.IsNullOrEmpty(user.WebSite) AndAlso _
Not String.IsNullOrEmpty(user.Email) AndAlso _
Not String.IsNullOrEmpty(user.About) AndAlso _
Not user.Region Is Nothing AndAlso _
Not user.BirthDate Is Nothing AndAlso _
Not user.isProfileComplete = True Then
_user.isProfileComplete = True
_user.Reputation = user.Reputation + 10
UserService.UpdateUser(_user)
Else
UserService.UpdateUser(_user)
End If
UserService.SubmitChanges()
Session("UserInfo") = Nothing
Return RedirectToAction("Details", "Users", New With {.id = id, .slug = user.UserName})
Else
Return View(user)
End If
End Function
在它进入服务进行验证之后,它被传递到存储库,看起来像这样
Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser
Dim _user = (From u In dc.Users
Where u.ID = user.ID
Select u).Single
With _user
.About = user.About
.BirthDate = user.BirthDate
.Email = user.Email
.isClosed = user.isClosed
.isProfileComplete = user.isProfileComplete
.RegionID = user.RegionID
.Reputation = user.Reputation
.UserName = user.UserName
.WebSite = user.WebSite
End With
End Sub
我遇到的问题是三个字段。
isProfileComplete
、isClosed
& Reputation
全部“更新”为“NULL”。
我什至尝试在视图中创建隐藏字段来包含数据,但我所做的一切似乎都无法正确传递这三个字段。所有其他字段更新都没有问题。
谁能告诉我我错过了什么?
I've got a Controller ActionResult that looks like this
Function Edit(ByVal user As Domain.User, ByVal id As Integer) As ActionResult
Dim _user As Domain.User = user
If ModelState.IsValid Then
If Not String.IsNullOrEmpty(user.UserName) AndAlso _
Not String.IsNullOrEmpty(user.WebSite) AndAlso _
Not String.IsNullOrEmpty(user.Email) AndAlso _
Not String.IsNullOrEmpty(user.About) AndAlso _
Not user.Region Is Nothing AndAlso _
Not user.BirthDate Is Nothing AndAlso _
Not user.isProfileComplete = True Then
_user.isProfileComplete = True
_user.Reputation = user.Reputation + 10
UserService.UpdateUser(_user)
Else
UserService.UpdateUser(_user)
End If
UserService.SubmitChanges()
Session("UserInfo") = Nothing
Return RedirectToAction("Details", "Users", New With {.id = id, .slug = user.UserName})
Else
Return View(user)
End If
End Function
After it goes to the Service for validation, it gets passed to the Repository which looks like this
Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser
Dim _user = (From u In dc.Users
Where u.ID = user.ID
Select u).Single
With _user
.About = user.About
.BirthDate = user.BirthDate
.Email = user.Email
.isClosed = user.isClosed
.isProfileComplete = user.isProfileComplete
.RegionID = user.RegionID
.Reputation = user.Reputation
.UserName = user.UserName
.WebSite = user.WebSite
End With
End Sub
The problem I'm having is with THREE fields.
isProfileComplete
, isClosed
& Reputation
are all getting "Updated" as "NULL".
I even tried creating hidden fields in the View to contain the data, but nothing I'm doing seems to properly pass those three fields. All other fields update without issue.
Can anyone tell me what I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
忘记这个...我想通了。
我错过了将以下字段放入表单中(我将它们放在表单的底部)
forget this one... I figured it out.
I missed putting the following fields into the form (I had them at the bottom, out of the form)