views:

713

answers:

3

I'm using jQuery autocomplete plugin from jQuery website calling the controller url which return json in return. The problem is the parameter sent to the controller is always null.

Here is the in-browser jQuery code for the autocomplete:

$(document).ready(function() {
    var url = "/Building/GetMatchedCities";
    $("#City").autocomplete(url);
});

and here is the ASPNET MVC controller signature in C#:

public JsonResult GetMatchedCities(string city)
{
    ..
    return this.Json(query, JsonRequestBehavior.AllowGet);
}

Thanks in advance,

Mohammad

+1  A: 

Try adding the city data as extraParms:

$("#City").autocomplete(url, {
extraParams: { city: $('#City').val() }
});

This is assuming the $('#City') is an input of type text when using the .val

-- Edited --

Based on your feedback the answer is:

The controller should be:

public JsonResult GetMatchedCities(string q)
{
    ..
    return this.Json(query, JsonRequestBehavior.AllowGet);
}

The jquery would be:

 $(document).ready(function() { 
    var url = "/Building/GetMatchedCities"; 
    $("#City").autocomplete(url); 
 });
amurra
The assumption that $('#City') is the input is correct, however "extraParams" option does again look into the same input and now sends empty string rather than null. Well, one step ahead, but it still sends the empty string.I sort of seeing the problem in the way MVC.NET handles the url, since with other methods it works fine according to sample examples on the website.
What is your route for /Building/GetMatchedCities in your global.asax? Do you have city defined as part of the route like /Building/GetMatchedCities/{city}?
amurra
previously I didn't add any route to the Global.asax and ran it with defualt. Now I've added the following route but city is null this time! routes.MapRoute( "GetCities", // Route name "{controller}/{action}/{city}", // URL with parameters new { controller = "Building", action = "GetMatchedCities", city = UrlParameter.Optional } // Parameter defaults );
Try adding city to your initial URL: var url = "/Building/GetMatchedCities/" + $('#City').val();
amurra
jQuery autocomplete sends two parameters(q, limited). It passes the value of city when I changed controller signature from JsonResult GetMatchedCitites(string city)to JsonResult GetMatchedCities(string q)I decided not to use json for this purpose and use Ajax instead returning a "ContentResult" type as the result of controller and it worked properly with jQuery autocomplete with it's simple syntax:$(document).ready(function() { var url = "/Building/GetMatchedCities"; $("#City").autocomplete(url);});
Ah sorry I misunderstood the city parameter then. You are definitely right in setting the parameter to q.
amurra
+1  A: 

When I did this, I specified the source option on autocomplete to be a function that called out to the ASPNET app. This allowed me to specify the URL directly. For you it would like this:

$("#City").autocomplete({
    source: function(req, responseFn) {
        addMessage("search on: '" + req.term + "'<br/>", true);

        $.ajax({
            url     : ajaxUrlBase1 + "GetMatchedCities/" + req.term,
            cache   : false,
            type    : "GET", // http method
            success : function(msg){
                // ajax call has returned
                var result = msg;
                if (result !== null){
                  var a = [];
                  for(var i=0; i < result.length; i++) {
                    a.push({label: result[i].cityname, id: result[i].abbrev});
                  }
                  responseFn(a);
                } else {
                  responseFn(null);
                }
            }
        });
    }
});

Of course, what you do inside the success fn would depend on the shape of the json you return from your Action.

Cheeso
I'm using the autocomplete from jquery site. I seems it doesn't have "source" option or I'm not looking enough! here is the link.http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions
There is more than one autocomplete http://stackoverflow.com/questions/2421966 . I don't know about that particular plugin. jQueryUI includes an "official" autocomplete widget as of... v1.8? ... and that is the one that I use with the above code. Find it at http://docs.jquery.com/UI/Autocomplete
Cheeso
+1  A: 

I had this same problem. After looking at the URL created by JQuery in Fiddler, I discovered that it looked like this: /MyController/MyMethod?term=x. So I changed my method signature to use the parameter name 'term' instead of 'q' ('q' is shown in the JQuery website autocomplete examples.). This fixed the problem and I was able to still return the JsonResult I needed.

    public JsonResult MyMethod(string term) 
    {
        ...
        return Json(query, JsonRequstBehavior.AllowGet);
    }
tjp69