Autocomplete control w/ MVC

Last post 04-07-2008 6:26 PM by Orion2480. 3 replies.

Sort Posts:

  • Autocomplete control w/ MVC

    04-03-2008, 5:31 PM
    • Loading...
    • Orion2480
    • Joined on 02-13-2008, 11:00 AM
    • Posts 40

    Hi all.  I'm trying to use the Autocomplete control from the AJAX toolkit in my MVC application.  And I can't seem to get it to work.  Following the example at http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AutoComplete/AutoComplete.aspx, I did the following:

    1) Created a new MVC project

    2) Added a script manager and autocomplete control inside my aspnetForm

    3) Added a method matching the signature defined on the webiste, specifically

     

    [WebMethod]
    [ScriptMethod]
    public string[] GetCompletionList(string prefixText, int count)
    {
        return new string[] { "aaabcdefghijk", "aaabc", "aaabcdefg" 
    }

     

    I've attempted every possible combination of ServiceMethod and ServicePath I can think of, putting my GetCompletionList() method both in my controller and in the code behind for the page.  I imagine the code behind is treated differently in MVC as compared to ASP.NET webforms approach, and since requests to the server are /controller/action, that the ServiceMethod and ServicePath properties need to point to something ni the controller, instead of the views code-behind.  But I just can't get the darn thing to work. 
     

    Any suggestions? 

  • Re: Autocomplete control w/ MVC

    04-04-2008, 4:37 AM
    • Loading...
    • maartenba
    • Joined on 01-23-2008, 1:27 PM
    • Posts 13

    Have you included the Ajax JavaScript references on your view? (i.e. all Ajax classes and proxy methods for this script reference)

  • Re: Autocomplete control w/ MVC

    04-04-2008, 12:06 PM
    • Loading...
    • Orion2480
    • Joined on 02-13-2008, 11:00 AM
    • Posts 40

    Yes, the JS is included in the page.

     I've made a little progress, but it's still not quite there.  It turns out your ServicePath needs to be "/controller" (the leading slash is important) and the ServiceMethod is "action". 

    Using Firebug in Firefox, I examined the response from the example/tutorial page of a working Autocomplete.  It appears the response is of type 'application/json', and the response content is a simple array, ["aaa", "aab"] etc.

    I've mimicked this behavior in my action, by manually setting the resopnse type to 'application/json' and then returning a json formatted string similar to that found in the working example.  However, it still doesn't work.  I get the JSON response just fine (inspected using Firebug), but nothing happens in Firefox.  In IE, I get a javascript error that the "Sys.Net.WebServiceFailedException: The server method 'GetCompletionList' returns invalid data.  The 'd' property is missing from the JSON wrapper"


    However, I see no "d" property in the response content of the working example. 

  • Re: Autocomplete control w/ MVC

    04-07-2008, 6:26 PM
    Answer
    • Loading...
    • Orion2480
    • Joined on 02-13-2008, 11:00 AM
    • Posts 40

    Okay, I got this to work, but here are the tricks involved:

    1) ScriptPath = "/controllername" (leading slash important)

    2) ScriptMethod = "actionname" 

    3) In your action, set the ContentType = "application/json"

    4) The response format needs to be as follows: (JSON formatted string)

    { "d" : ["array", "of", "possible", "matches" ] }

    Even though, using firebug, the response format from the demo website does not show this "d" property, without it I would get a JS error in IE that the "d" property is undefined.  As far as I can gather, this is done for security reasons, though how this makes the exchange more secure I have no idea. 

    5) Don't do RenderView(), instead use Response.Write(), and write out the JSON formatted string from item #4.

Page 1 of 1 (4 items)