jQuery autocomplete将null参数传递给ASP.NET MVC 2中的控制器

时间:2022-08-24 22:08:59

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.

我正在使用jQuery网站上的jQuery autocomplete插件调用控制器url,返回json。问题是发送到控制器的参数始终为空。

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

以下是自动完成的浏览器内jQuery代码:

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

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

这是C#中的ASPNET MVC控制器签名:

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

Thanks in advance,

提前致谢,

Mohammad

*

3 个解决方案

#1


4  

Try adding the city data as extraParms:

尝试将城市数据添加为extraParms:

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

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

这假设$('#City')是使用.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:

jquery将是:

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

#2


9  

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.

我有同样的问题。在Fiddler中查看JQuery创建的URL之后,我发现它看起来像这样:/ MyController / MyMethod?term = x。所以我改变了我的方法签名,使用参数名称'term'而不是'q'('q'显示在JQuery网站自动完成示例中。)。这解决了问题,我仍然能够返回我需要的JsonResult。

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

#3


2  

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:

当我这样做时,我指定自动完成的源选项是一个调用ASPNET应用程序的函数。这允许我直接指定URL。对你来说,它想要这样:

$("#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.

当然,你在成功fn中所做的事情将取决于你从Action返回的json的形状。

#1


4  

Try adding the city data as extraParms:

尝试将城市数据添加为extraParms:

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

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

这假设$('#City')是使用.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:

jquery将是:

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

#2


9  

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.

我有同样的问题。在Fiddler中查看JQuery创建的URL之后,我发现它看起来像这样:/ MyController / MyMethod?term = x。所以我改变了我的方法签名,使用参数名称'term'而不是'q'('q'显示在JQuery网站自动完成示例中。)。这解决了问题,我仍然能够返回我需要的JsonResult。

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

#3


2  

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:

当我这样做时,我指定自动完成的源选项是一个调用ASPNET应用程序的函数。这允许我直接指定URL。对你来说,它想要这样:

$("#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.

当然,你在成功fn中所做的事情将取决于你从Action返回的json的形状。