我不知道我做错了什么,或者是什么导致了这个错误。当我将此表单提交给控制器时,控制器会收到 2 个空字段:
这是视图内的表单:
@Using Html.BeginForm("contactSearch", "search", FormMethod.Post)
@Html.TextBox("SearchString")
@Html.DropDownList("opportunite", "Choisissez une opportunité")
@<input type="submit" value="Submit!" />
End Using
这是控制器:
<HttpPost()>
Function contactSearch(ByVal search As String, ByVal opportunite As opportunite) As ViewResult
If opportunite Is Nothing Then
ModelState.AddModelError("opportunite", "Opportunite is empty")
ViewBag.opportunite = New SelectList(db.opportunite, "idOpportunite", "nomOffre")
Return View()
End If
...
我还有一个小问题,如果我写:
Function contactSearch(ByVal searchString As String, ByVal opportunite As Integer)
如果其中一个字段为空,我会收到此错误:参数字典包含参数“机会”的空条目...
所以,我必须解决这个问题的唯一方法是使用下面的代码。没有更好的方法来解决这个问题吗?
Function contactSearch(ByVal search As String, ByVal opportunite As opportunite) As ViewResult
Dim opport As Integer = 0
Try
opport = Val(Request("opportunite"))
Catch ex As Exception
End Try
If opport = 0 Then