尝试让 JQuery 自动完成在 Asp.Net 页面上工作

发布于 2024-08-26 05:44:06 字数 1123 浏览 6 评论 0原文

有人可以阐明这个问题吗:

我有以下内容:

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

在我的 Asp.Net 页面上。 http请求是MVC控制器上的一个函数,代码在这里:

    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

据我所知,控制器正在返回JSON数据,但是我不知道函数参数是否正确,或者返回的格式是否正确可由自动完成插件解释。

如果有人可以在此事上提供帮助,我将非常感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难愈 2024-09-02 05:44:07

几乎可以正常工作了,当然现在没有出现任何错误,但同样没有得到任何结果。

我现在使用的是新版本的 JQuery,它内置了 AutoComplete,并且现在在文本框中获得了我之前没有获得的动画轮 - 所以希望这是一个好兆头。

我的 MVC 功能是:

    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

aspx 页面上的脚本是:

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

同样,“minLength”参数似乎也工作正常,因为动画轮在我输入第二个字符之前不会“启动”。

任何额外的指示将不胜感激。

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.

著墨染雨君画夕 2024-09-02 05:44:07

该函数需要接受一个名为 q 的参数,其中包含搜索文本。您可以使用 extraParams 选项传递计数,但默认情况下不会传递。

我在 MVC 控制器操作中返回的内容实际上是使用 StringBuilder.AppendLine 为搜索结果中的每条记录构建的新行分隔列表。

HTH

这是 MVC 中使用的一个非常好的示例 链接

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 Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文