views:

606

answers:

2

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?

+3  A: 

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:

  1. Accepting a JSON string.
  2. Only requiring 2 script references (jQuery and the AutoComplete script) rather than the 7 I needed for the ASP.NET AJAX option.
  3. I can easily get minified versions of both jQuery and AutoComplete, but the ClientSide only scripts in the ASP.NET AJAX download are non-minified (unless I've missed something).

Obviously if you're already loading the framework elsewhere for other things, then your mileage may vary.

Zhaph - Ben Duguid
I used jQuery instead. It worked well. http://stackoverflow.com/questions/1533761/jquery-email-address-input/1533790#1533790
Zack Peterson
+1  A: 

If the idea of consuming an ASMX web service is an issue, another alternative is to configure the autocomplete control to call a page method, which can reside in the code behind of the page (or control) in question.

An overview of how to set this up can be found here.

Dillie-O