为什么这段代码不适用于另一种解决方案?

时间:2009-10-23 10:36:05

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

我在当前的解决方案中有以下代码,它返回错误“值''无效'。下面的代码片段已缩短为仅显示问题区域而不是整个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)

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

一切都收到了值。

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

我对此感到有点失落。

有人有什么想法吗?


编辑作为参考,这是整个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字段在表单上留空,那么只有在其中存在某些内容时才会抛出错误。

编辑这一行的更新,这一行:

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

现在读取

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

如果wmd-input框中没有任何内容,则页面会更新,但如果有,则返回The value '' is invalid.

2 个答案:

答案 0 :(得分:0)

您可能错过了参考资料。或者框架版本不同。

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

答案 1 :(得分:0)

我设法解决了这个问题,问题实际上存在于hdCalls和hdComments之间的Foriegn Key Contraints中。

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