将对象从控制器传递到存储库时数据库更新错误的值

发布于 2024-09-07 21:29:56 字数 1905 浏览 3 评论 0原文

我有一个控制器 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

我遇到的问题是三个字段。

isProfileCompleteisClosed & 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的夜不怕黑 2024-09-14 21:30:02

忘记这个...我想通了。

我错过了将以下字段放入表单中(我将它们放在表单的底部)

<%: Html.HiddenFor(Function(model) model.isProfileComplete)%>
<%: Html.HiddenFor(Function(model) model.isClosed)%>
<%: Html.HiddenFor(Function(model) model.Reputation)%>

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)

<%: Html.HiddenFor(Function(model) model.isProfileComplete)%>
<%: Html.HiddenFor(Function(model) model.isClosed)%>
<%: Html.HiddenFor(Function(model) model.Reputation)%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文