[英]asp.net mvc autocomplete using jquery
I know that my question as already post in the forum, but i found no working solution. 我知道我的问题已经在论坛上发布了,但是我没有找到有效的解决方案。 I am sure that i am doing something wrong but i am unable to find. 我确定我做错了事,但找不到。
here is a test code who work and return me a list from the datatable 这是一个测试代码,可以工作,并向我返回数据表的列表
<TestMethod()> Public Sub ObtientNomsRestaurants()
Dim q = From c In dal.ObtientNomsRestaurants() 'As IQueryable of Resto
For Each resto In q
Console.WriteLine(resto.Nom)
Next
End Sub
now in my controller, i call the dal method and try to return all element from my datatable column "Nom" that start with the parameter "Prefix" 现在在我的控制器中,我调用dal方法并尝试从我的数据表列“ Nom”返回以参数“ Prefix”开头的所有元素
<HttpPost>
Public Function TagSearch(Prefix As String) As JsonResult
Dim q = From c In dal.ObtientNomsRestaurants().Where(Function(m) m.Nom.StartsWith(Prefix)).Select(Function(m) New With {.Name = m.Nom}).ToList
Return Json(q, JsonRequestBehavior.AllowGet)
End Function
To finish in my view, i have an input textbox with "tags" as id and this autocomplete 为了完成我的观点,我有一个输入文本框,其中“ ids”为id,并且此自动完成功能
<script type="text/javascript">
$(function () {
$("#tags").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Home/TagSearch",
type: "POST",
dataType: "json",
data: { Prefix: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item.Name, value: item.Name };
}))
}
})
},
messages: {
noResults: "", results: ""
}
});
});
</script>
Edit: So now i edit my controller and this is working 编辑:所以现在我编辑我的控制器,这正在工作
<HttpPost>
Public Function TagSearch(Prefix As String) As JsonResult
'Dim ObjList = dal.ObtientTousLesRestaurants()
Dim ObjList As New List(Of Resto)() From {
New Resto() With {.Id = 4, .Nom = "Latur"},
New Resto() With {.Id = 5, .Nom = "Mumbai"},
New Resto() With {.Id = 6, .Nom = "Pune"},
New Resto() With {.Id = 7, .Nom = "Delhi"},
New Resto() With {.Id = 8, .Nom = "Dehradun"},
New Resto() With {.Id = 9, .Nom = "Noida"},
New Resto() With {.Id = 10, .Nom = "New Delhi"}
}
Dim Nom = (From N In ObjList Where N.Nom.StartsWith(Prefix) Select New With {N.Nom})
Return Json(Nom, JsonRequestBehavior.AllowGet)
End Function
but if i use the dal method to get database data, its not working. 但是,如果我使用dal方法获取数据库数据,则无法正常工作。 In both way, ObjList is a List Of Resto. 无论哪种方式,ObjList都是Resto列表。
我解决了谁在connectionString中的问题,请参阅此处的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.