Can I make an ASP.NET AJAX AutoCompleteExtender use an ASP.NET MVC JsonResult rather than an ASMX Webservice?
I’ve got an ASP.NET AJAX Toolkit AutoCompleteExtender on an ASP.NET MVC View. It uses an JsonResult type function in my MVC Controller.
ASP.NET MVC View:
<form runat="server">
<asp:ScriptManager
ID="ScriptManager1"
runat="server"
EnableScriptGlobalization="true" />
<ajaxToolkit:AutoCompleteExtender
ID="autoComplete1"
runat="server"
TargetControlID="TextBox1"
ServiceMethod="find"
ServicePath="/thing"
MinimumPrefixLength="1"
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="20"
DelimiterCharacters=";"
ShowOnlyCurrentWordInCompletionListItem="true" />
<asp:TextBox
ID="TextBox1"
runat="server" />
</form>
ASP.NET MVC Controller:
<AcceptVerbs(HttpVerbs.Post)> _
Function Find(ByVal collection As FormCollection) As JsonResult
Dim search As String = collection(0)
Dim j As New JsonResult
j.Data = ...
Return j
End Function
This fails because collection.Count is 0. Also, Request.QueryString.Count is 0.
How do I pass the typed string to my Find() function?
Sadly, the ASP.NET AJAX AutoComplete Extended requires a SOAP web service.
That said, the source is available on CodePlex, so you could probably modify it to take a JSON string.
Personally, as I was using jQuery elsewhere on a site, I decided not to use the ASP.NET AJAX stuff and go with a jQuery plugin (jQuery.Autocomplete)
This had the additional benefits of:
Obviously if you’re already loading the framework elsewhere for other things, then your mileage may vary.