Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery autocomplete pass null parameter to the controller in ASP.NET MVC 2

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

like image 447
Mohammad Avatar asked Mar 25 '10 15:03

Mohammad


1 Answers

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);
    }
like image 123
tjp69 Avatar answered Oct 02 '22 10:10

tjp69