【发布时间】: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