尝试让 JQuery 自动完成在 Asp.Net 页面上工作
有人可以阐明这个问题吗:
我有以下内容:
$(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几乎可以正常工作了,当然现在没有出现任何错误,但同样没有得到任何结果。
我现在使用的是新版本的 JQuery,它内置了 AutoComplete,并且现在在文本框中获得了我之前没有获得的动画轮 - 所以希望这是一个好兆头。
我的 MVC 功能是:
aspx 页面上的脚本是:
同样,“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:
and the script on the aspx page is:
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.
该函数需要接受一个名为 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