使用 jQuery getJson 将列表/数组作为参数发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3676883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send list/array as parameter with jQuery getJson
提问by fearofawhackplanet
I have the following where I'm trying to send list/array to MVC controller method:
我有以下尝试将列表/数组发送到 MVC 控制器方法的地方:
var id = [];
var inStock = [];
$table.find('tbody>tr').each(function() {
id.push($(this).find('.id').text());
inStock.push($(this).find('.stocked').attr('checked'));
});
var params = {};
params.ids = id;
params.stocked = inStock;
$.getJSON('MyApp/UpdateStockList', params, function() {
alert('finished');
});
in my contoller:
在我的控制器中:
public JsonResult UpdateStockList(int[] ids, bool[] stocked) { }
both paramaters are null.
两个参数都为空。
Note that if I change the params to single items
请注意,如果我将参数更改为单个项目
params.ids = 1;
params.stocked = true;
public JsonResult UpdateStockList(int ids, bool stocked) { }
then it works ok, so I don't think it's a routing issue.
然后它工作正常,所以我认为这不是路由问题。
回答by Darin Dimitrov
Try setting the traditional
flag:
尝试设置traditional
标志:
$.ajax({
url: '/home/UpdateStockList',
data: { ids: [1, 2, 3], stocked: [true, false] },
traditional: true,
success: function(result) {
alert(result.status);
}
});
works fine with:
工作正常:
public ActionResult UpdateStockList(int[] ids, bool[] stocked)
{
return Json(new { status = "OK" }, JsonRequestBehavior.AllowGet);
}
回答by hippietrail
Besides calling .ajax()
instead of .getJSON()
as Darin suggests or setting the global jQuery.ajaxSettings.traditional
to true
as jrduncans suggests, you can also pass the result of calling the jQuery .param()
functionon your params
object:
除了要求.ajax()
,而不是.getJSON()
作为达林的建议,或设置全局jQuery.ajaxSettings.traditional
,以true
作为jrduncans建议,你也可以通过调用的结果jQuery的.param()
功能,您的params
对象:
var id = [];
var inStock = [];
$table.find('tbody>tr').each(function() {
id.push($(this).find('.id').text());
inStock.push($(this).find('.stocked').attr('checked'));
});
var params = {};
params.ids = id;
params.stocked = inStock;
$.getJSON('MyApp/UpdateStockList', $.param(params, true), function() {
alert('finished');
});
回答by jrduncans
Unfortunately, while it seems that jquery provides a "traditional" flag to toggle this behavior on jQuery.ajax, it does not on jQuery.getJSON. One way to get around this would to be set the flag globally:
不幸的是,虽然 jquery 似乎提供了一个“传统”标志来切换 jQuery.ajax 上的这种行为,但它没有在 jQuery.getJSON 上。解决此问题的一种方法是全局设置标志:
jQuery.ajaxSettings.traditional = true;
jQuery.ajaxSettings.traditional = true;
See the documentation for jQuery.param: http://api.jquery.com/jQuery.param/Also see the release notes for this change: http://jquery14.com/day-01/jquery-14(search for 'traditional')
请参阅 jQuery.param 的文档:http: //api.jquery.com/jQuery.param/另请参阅此更改的发行说明:http: //jquery14.com/day-01/jquery-14(搜索 '传统的')
回答by SharpC
In the view, generate multiple named fields(not id
, as id
should be unique per field), noting the use of Name
not name
:
在视图中,生成多个命名字段(not id
,因为id
每个字段应该是唯一的),注意使用Name
notname
:
@foreach (var item in Model.SomeDictionary)
{
@Html.TextBoxFor(modelItem => item.Value.SomeString, new { Name = "someString[]" })
}
Then retrieve the input field values using jQuery, so:
然后使用 jQuery 检索输入字段值,因此:
var myArrayValues = $('input[name="someString[]"]').map(function () { return $(this).val(); }).get();
You can use this directly in jQuery / AJAX as follows:
您可以直接在 jQuery / AJAX 中使用它,如下所示:
$.ajax({
type: "POST",
url: "/MyController/MyAction",
dataType: 'json',
data: {
someStrings: $('input[name="someString[]"]').map(function () { return $(this).val(); }).get(),
someDates: $('input[name="someDate[]"]').map(function () { return $(this).val(); }).get(),
Then in the controller action in MVC:
然后在 MVC 中的控制器操作中:
[HttpPost]
public JsonResult MyAction(string[] someStrings, DateTime[] someDates...