I don’t know what I’ve done wrong, or what could have caused this bug. When I submit this form to the controller, the controller receives 2 empty fields:
Here is the form inside the view:
@Using Html.BeginForm("contactSearch", "search", FormMethod.Post)
@Html.TextBox("SearchString")
@Html.DropDownList("opportunite", "Choisissez une opportunité")
@<input type="submit" value="Submit!" />
End Using
And here is the controller:
<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
...
I Still have a little problem, If I write :
Function contactSearch(ByVal searchString As String, ByVal opportunite As Integer)
And if one of those fields is empty, I got this error : The parameters dictionary contains a null entry for parameter ‘opportunite’…
So, the only way I have to solve this was to use the code below. Isn’t there any better way to solve this?
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
You incorrectly named your action arguments. Should be like this:
Notice that the 2 arguments are of type String. In your code you have used some custom
opportunitetype for the second argument but this doesn’t make sense because a DropDownList sends only the selected value. You cannot expect it to bind to a complex type.