Using the AutoComplete UI with Asp.Net, MVC and VB.Net
Can someone please offer some help/advice with the following:
I currently have the following block of Script on my .Aspx page:
- $(document).ready(function () {
$("#txtFirstContact").autocomplete({ source: 'http://localhost:7970/Home/FindSurname/',
minLength: 3
});
});
And I have the block of code on the MVC Controller:
- Function FindSurname(ByVal q As String, ByVal limit As Integer) As JsonResult
Dim sqlConnection As New SqlClient.SqlConnection
sqlConnection.ConnectionString = My.Settings.sqlConnection
Dim sqlCommand As New SqlClient.SqlCommand
sqlCommand.CommandText = "SELECT TOP " & limit & " ConSName FROM tblContact WHERE ConSName LIKE '" & q & "%'"
sqlCommand.Connection = sqlConnection
Dim ds As New DataSet
Dim da As New SqlClient.SqlDataAdapter(sqlCommand)
da.Fill(ds, "Contact")
sqlConnection.Close()
Dim a As New List(Of String)
For Each dr As DataRow In ds.Tables("Contact").Rows
a.Add(dr.Item("ConSName"))
Next
Return Json(a, JsonRequestBehavior.AllowGet)
End Function
I'm using the latest JQuery and JQuery UI libraries.
The problem is that after the 3rd character is typed in a textbox (on a JQuery Modal Dialog), the spinning indicator appears but nothing appears in the AutoComplete dropdown.
I've seen many many examples dotted over the Internet and as far as I can see, I'm coding everything proper but just not getting any results.
Running the MVC Controller Function direct does in fact return results.
I have seen various people give various parameters for the controller though, sometimes it's "value" or "q" or "term" and even the 2nd parameter I've seen as "Count" or "Limit".
Can someone please tell me where I may be going wrong?
Many thanks.