4

Okay, I've seen tons of questions posted regarding this question, but none of the answers has actually worked for me, here's my AJAX:

$.ajax({
        url: "/FilterSessions/GetFilterSession",
        type: "GET",
        dataType: "json",
        data: jsonFilters,
        traditional: true,
        success: function (response) {
            //Haha, it's never entering here. not really.
        }
    });

var "jsonFilters" contains an array with the following data:

[0] = { Path: "Test", Name: "More testing", Value: "Test Value" },
[1] = { Path: "Test", Name: "More testing", Value: "Test Value" } 

And this is my controller:

public ActionResult GetFilterSession(List<FilterSessionModel> jsonFilters)
{
    //Do things

    return Json(false, JsonRequestBehavior.AllowGet);
}

jsonFilters always remains null... I have also tried adding contentType: "application/json; charset=utf-8" to the AJAX call... but that didn't really do anything

Finally, the class FilterSessionModel is structured as follows:

 public class FilterSessionModel
    {
        public string Path { get; set; }
        public string Name { get; set; }
        public string Value { get; set; }
    }

Any ideas as to what I might be missing or what might be happening?

Things I've tried so far:

Setting "traditional: true", setting "contentType", using JSON.stringify and attempting to accept a string in the MVC Controller (no-go)

UPDATE: Thanks to the answer below I realized that what was missing was to send over the data with the param Id like so:

 data: "{param1ID:"+ param1Val+"}"
4

5 回答 5

1

我会尝试在您的操作中切换类型。

List<FilterSessionModel>

很确定上述方法行不通,我会尝试类似 Object.

或者可能是一个字符串,然后我将使用 newton json dll 将其推送到您的类列表中。

问题归结为您的操作无法确定类型,假设您在调用 ajax 之前检查您的数据。

**由于更多信息而更新。添加错误部分并在从控制器返回时查看这些变量,同时启动 fiddler 并观察你得到的 http 号码。

$.ajax({
    type: "POST",
    url: "Servicename.asmx/DoSomeCalculation", 
  data: "{param1ID:"+ param1Val+"}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        UseReturnedData(msg.d);
    },
    error: function(x, t, m, b) {
        //Look at the vars above to see what is in them.
    }
});
于 2013-09-14T23:38:58.140 回答
1

javascript 或 ajax 调用永远不会对对象进行类型转换。. .您需要将控制器端参数的类型设置为字符串或列表,否则您还可以设置对象类型。. 如果您以这种方式修改代码..您的代码绝对有效!

于 2013-09-23T11:59:47.013 回答
0

首先,我假设您$.ajax是针对JQuery而不是其他一些 Javascript 框架的假设。如果那是错误的,请纠正我。

ASP.NET MVC 实际上可以执行您的要求(将通过 AJAX 发送到的数据解析到 a List<FilterSessionModel>,但通过 GET 请求执行此操作似乎很困难。这将有助于了解您的 ASP.NET MVC 版本正在使用,因为需要更多才能使其在旧版本上运行.但是,我的建议应该适用于 MVC 3 或 4.

当您使用 GET 请求通过 JQuery 发送 AJAX 并将其传递给 JavaScript 数组时,这就是您发送到服务器的内容:

http://localhost:50195/FilterSessions/GetFilterSession?undefined=&undefined=

难怪模型为空,因为实际上没有发送数据。

我相信 ASP.NET 可以接受这样的对象(甚至是对象数组),但它不会接受格式化为 JSON(如通过 JSON.stringify)的格式,因为这只会导致以下请求:

http://localhost:50195/FilterSessions/GetFilterSession?[{%22Path%22:%22Test%22,%22Name%22:%22TestName%22,%22Value%22:%22Testing%22},{%22Path%22:%22Test%22,%22Name%22:%22TestName%22,%22Value%22:%22Testing%22}]

您可能想要执行此操作的方式是使用 POST 请求。ASP.NET MVC 实际上将接受 JSON 字符串作为 POST 数据,并将对其进行解码并正确解析模型。您的 AJAX 代码经过一些修改即可正常工作:

$.ajax({
    url: "/FilterSessions/GetFilterSession",
    type: "POST", //Changed to POST
    dataType: "json",
    data: JSON.stringify(jsonFilters), //Pack data in a JSON package.
    contentType: "application/json; charset=utf-8", //Added so ASP recognized JSON
    traditional: true,
    success: function (response) {
        alert('Success!');
    }
});

您发布的控制器应该已经识别 POST 数据,但如果它不能识别,[HttpPost]您只需要一个简单的属性:

[HttpPost]
public ActionResult GetFilterSession(List<FilterSessionModel> jsonFilters)
{
    //Do things

    return Json(false, JsonRequestBehavior.AllowGet);
}
于 2013-09-15T01:20:42.103 回答
0

我认为您正在寻找的答案在这里:

使用 jQuery Ajax 将对象列表传递给 MVC 控制器方法

于 2013-10-23T12:19:52.743 回答
0
$.ajax({
    url: "/FilterSessions/GetFilterSession",
    type: "GET",
    dataType: "json",
    data:JSON.stringify({ 'jsonFilters': jsonFilters}),
    contentType: 'application/json; charset=utf-8',
    success: function (response) {
        //Do your action
    }
});
于 2017-01-10T15:03:28.340 回答