使用jQuery ajax / load提交数组参数

public ActionResult DoSomething(string[] arr, bool someBool, int someInt) { }

试图从jQuery调用上面的方法:

 var test = []; test.push('dog'); test.push('cat'); $container.load('MyController/DoSomething', { 'arr[]': test, 'someBool': true, 'someInt': 1 }, function(response, status, xhr) { // ... }); 

数组参数为空,其他参数都很好。 我究竟做错了什么?

Chrome开发者工具会将提交的表单数据显示为

 arr%5B%5D%5B%5D:dog arr%5B%5D%5B%5D:cat someBool:true someInt:1 

不知道那里发生了什么,但看起来不对我

如果您使用的是jquery 1.4,则可能需要将traditional参数设置为true ,以便与ASP.NET MVC中的默认模型绑定器格式兼容:

 var test = []; test.push('dog'); test.push('cat'); $.ajax({ url: 'MyController/DoSomething', type: 'GET', traditional: true, data: { arr: test, someBool: true, someInt: 1 }, success: function(result) { $container.html(result); } }); 

或者如果您更喜欢.load()方法:

 var data = { arr: test, someBool: true, someInt: 1 }; $container.load('MyController/DoSomething', $.param(data, true), function(response, status, xhr) { // ... }); 

只需删除[]

 { 'arr': test, 'someBool': true, 'someInt': 1 }, 

发布值(使用Firebug进行检查)。

 arr[] dog arr[] cat someBool true someInt 1 
  • 关于jsFiddle的示例

你能看出这个问题是否与你的问题类似:

使用jQuery的$ .ajax将嵌套数组传递给asp.net mvc

即使我面临错误,将数组从HTML页面传递到aspx页面。

我的要求是将aspx页面加载到html页面的DIV标记中。 在页面加载我需要将这些JS数组值传递给aspx页面加载。

我用下面的方法。

$('#').load("Targetpage.aspx",{"Arr":JSArrValues});

在aspx页面加载事件中,我可以访问以下值:

string results = Response["Arr[]"];

感谢JQuery API文档, 在此处输入链接描述和stackoverflow