views:

132

answers:

2

Can someone shed some light on the problem please:

I have the following:

    $(document).ready(function () {
    $("#txtFirstContact").autocomplete({url:'http://localhost:7970/Home/FindSurname' });
});

On my Asp.Net page. The http request is a function on an MVC Controller and that code is here:

    Function FindSurname(ByVal surname As String, ByVal count As Integer)
    Dim sqlConnection As New SqlClient.SqlConnection
    sqlConnection.ConnectionString = My.Settings.sqlConnection
    Dim sqlCommand As New SqlClient.SqlCommand

    sqlCommand.CommandText = "SELECT ConSName FROM tblContact WHERE ConSName LIKE '" & surname & "%'"

    sqlCommand.Connection = sqlConnection

    Dim ds As New DataSet
    Dim da As New SqlClient.SqlDataAdapter(sqlCommand)
    da.Fill(ds, "Contact")
    sqlConnection.Close()

    Dim contactsArray As New List(Of String)
    For Each dr As DataRow In ds.Tables("Contact").Rows
        contactsArray.Add(dr.Item("ConSName"))
    Next
    Return Json(contactsArray, JsonRequestBehavior.AllowGet)

End Function

As far as I'm aware, the Controller is returning JSON data, however I don't know if the Function Parameters are correct, or indeed if the format returned is interprettable by the AutoComplete plugin.

If anyone can assist in the matter I'd really appreciate it.

A: 

The function needs to accept a parameter named q which contains the search text. You can pass the count in using the extraParams option but it's not passed by default.

The content I return in my MVC controller actions is actually a new line delimited list built using a StringBuilder.AppendLine for each record in the search results.

HTH

This is a very good example of usage within MVC http://geekswithblogs.net/renso/archive/2009/09/08/jquery-autocomplete-in-asp.net-mvc-framework.aspx

Dan Kennedy
Thanks Dan, what version of JQuery are you using or indeed what autocomplete plugin because what you've said makes total sense but it's just not working for me and wondering if I've got a different plugin and/or version to you.
JasonMHirst
I'm using jQuery 1.3.2, but there's no version number on the jquery.ui.autocomplete.js file but the file header contains the following lineRevision: $Id: jquery.autocomplete.js 5785 2008-07-12 10:37:33Z joern.zaefferer $Dunno if that helps at all
Dan Kennedy
Thanks Dan, really appreciate your help. Not resolved at the moment but will grab a coffee and continue the research. Again my thanks.
JasonMHirst
No worries. If you still can't get it to work post a comment back and I'll see if I can knock up a working sample.
Dan Kennedy
A: 

Hi Dan,

Almost got this working, certainly not getting any errors now but likewise not getting any results.

I've now using the new version of JQuery which has AutoComplete built in, and do now get the animated wheel in the textbox which I wasn't getting before - so hopefully that's a good sign.

My MVC Function is:

    Function FindSurname(ByVal q As String, ByVal limit As Integer) As String
    Dim sqlConnection As New SqlClient.SqlConnection
    sqlConnection.ConnectionString = My.Settings.sqlConnection
    Dim sqlCommand As New SqlClient.SqlCommand

    sqlCommand.CommandText = "SELECT 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 StringBuilder
    For Each dr As DataRow In ds.Tables("Contact").Rows
        a.Append(dr.Item("ConSName"))
        a.AppendLine()
    Next
    Return a.ToString
End Function

and the script on the aspx page is:

    $(document).ready(function () {
    $("#txtFirstContact").autocomplete({    source: 'http://localhost:7970/Home/FindSurname/',
                                            minLength: 2
                                        });
});

Again, the "minLength" parameter seems to be working fine too as the animated wheel doesn't 'kick in' until I've entered the 2nd character.

Any additional pointers would be very appreciated.

JasonMHirst