【问题标题】:Why doesn't this code work in another solution?为什么这段代码在另一个解决方案中不起作用?
【发布时间】:2023-04-05 15:32:01
【问题描述】:

我当前的解决方案中有以下代码,它返回错误“值”无效”。下面的 sn-p 已被缩短,仅显示问题区域,而不是整个 ActionResult。

        Dim tComment As New hdComment

        tComment.Comment = collection("wmd-input")
        tComment.MadeOn = DateTime.Now
        tComment.UserID = Session("LoggedInUser")
        tComment.CallID = id

        If Not tComment.Comment.Trim().Length = 0 Then
            db.hdComments.InsertOnSubmit(tComment)
        End If

        db.SubmitChanges()

        Return Redirect("/Calls/Details/" & id)

但是,在之前的项目中,我使用了完全相同的代码,即使视图相同,但仍然返回上述错误。

一切正常。

唯一不同的是它是一个不同的项目。

我对这个有点不知所措。

有人有什么想法吗?


EDIT作为参考,这里是整个ActionResult。

'
' POST: /Calls/Details/5
<Authorize()> _
<AcceptVerbs(HttpVerbs.Post)> _
<ValidateInput(False)> _
Function Details(ByVal id As Integer, ByVal collection As FormCollection) As ActionResult

    Dim calls As hdCall = callRepository.GetCall(id)

    ViewData("MyCallID") = calls.CallID
    ViewData("UserThatLogged") = calls.UserID

    ViewData("TimeLogged") = calls.loggedOn.ToLongDateString & " " & calls.loggedOn.ToLongTimeString
    ViewData("Title") = calls.Title

    Dim dataContext As New CustomerServiceModelDataContext
    ViewData("Status") = New SelectList(dataContext.hdStatus, "StatusID", "Status", calls.StatusID)
    ViewData("Type") = New SelectList(dataContext.hdCategories, "CategoryID", "Title", calls.CategoryID)
    ViewData("Company") = calls.hdCompany.Company
    ViewData("Priority") = New SelectList(dataContext.hdPriorities, "PriorityID", "Priority", calls.PriorityID)
    ViewData("CallDetails") = calls.CallDetails
    ViewData("Customer") = calls.hdCustomer.CustomerName
    ViewData("CustomerID") = calls.hdCustomer.CustomerID
    ViewData("CustomerCallCount") = callRepository.CountCallsForThisCustomer(calls.hdCustomer.CustomerID).Count()
    ViewData("ContactNumber") = calls.hdCustomer.Telephone
    ViewData("AssignedTo") = New SelectList(dataContext.aspnet_Users, "UserName", "UserName", calls.AssignedTo)


    Dim callComments = callRepository.GetCallComments(id)
    Dim db As New CustomerServiceModelDataContext

    Try
        Dim tComment As New hdComment

        tComment.Comment = collection("wmd-input")
        tComment.MadeOn = DateTime.Now
        tComment.UserID = Session("LoggedInUser")
        tComment.CallID = id

        If Not tComment.Comment.Trim().Length = 0 Then
            db.hdComments.InsertOnSubmit(tComment)
        End If

        'Update any call changes
        Dim tCall = (From c In db.hdCalls _
                     Where c.CallID = id _
                     Select c).SingleOrDefault


        tCall.updatedOn = DateTime.Now
        tCall.UpdatedBy = Session("LoggedInUser")
        tCall.StatusID = collection("Status")
        tCall.AssignedTo = collection("AssignedTo")
        tCall.CategoryID = collection("Type")
        tCall.PriorityID = collection("Priority")

        db.SubmitChanges()

        Return Redirect("/Calls/Details/" & id)

    Catch ex As Exception
        ModelState.AddModelError("Error", ex)
        Return View(callComments)
    End Try

    Return View(callComments)
End Function

其余的代码可以正常工作,如果 wmd-input 字段在表单上留空,只有当其中有内容时才会抛出错误。

EDIT 对此行的一点更新:

If Not tComment.Comment.Trim().Length = 0 Then

现在读取

If (Not tComment.Comment.Trim().Length = 0) Then

如果 wmd 输入框中没有内容,则页面更新,但如果有,则返回 The value '' is invalid.

【问题讨论】:

  • 您确认 Session("LoggedInUser") 是正确的吗?
  • 是的,这个问题似乎只来自tComment.Comment = collection("wmd-input")If Not tComment.Comment.Trim().Length = 0 Then db.hdComments.InsertOnSubmit(tComment) End If
  • tComment.Comment的数据类型是什么?
  • 它的数据类型是一个字符串。
  • 而且它在数据库中的datatype也是接受字符串作为输入的类型吧?

标签: asp.net asp.net-mvc linq


【解决方案1】:

您可能缺少参考。或者框架版本不同。

也是同一台开发机,两个地方都安装了asp.net-mvc吗?

【讨论】:

  • 是的,与 ASP.NET MVC 1.0 相同的机器。
  • 你能检查一下这两个项目的引用是否有差异。
  • 是的,引用是相同的。
【解决方案2】:

我设法解决了这个问题,问题实际上在于 hdCalls 和 hdComments 之间的外键约束。

我删除了约束并重新创建了它们,突然之间就很好了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 2010-09-18
    相关资源
    最近更新 更多