使用jQuery getJson将列表/数组作为参数发送

时间:2021-07-20 10:35:31

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.

这两个参数是null。

Note that if I change the params to single items

注意,如果我将params更改为单个项目

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.

然后它就可以工作了,所以我不认为这是一个路由问题。

4 个解决方案

#1


39  

Try setting the traditional flag:

尝试设置传统的旗帜:

$.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);
}

#2


18  

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() function on your params object:

除了调用.ajax()而不是Darin建议的. getjson()或设置全局jQuery.ajaxSettings之外。按照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');
});    

#3


6  

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。传统= 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/jquery14(搜索“传统”)

#4


0  

In the view, generate multiple named fields (not id, as id should be unique per field), noting the use of Name not name:

在视图中,生成多个命名字段(不是id,因为每个字段的id应该是唯一的),注意名称而不是名称的使用:

@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...

#1


39  

Try setting the traditional flag:

尝试设置传统的旗帜:

$.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);
}

#2


18  

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() function on your params object:

除了调用.ajax()而不是Darin建议的. getjson()或设置全局jQuery.ajaxSettings之外。按照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');
});    

#3


6  

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。传统= 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/jquery14(搜索“传统”)

#4


0  

In the view, generate multiple named fields (not id, as id should be unique per field), noting the use of Name not name:

在视图中,生成多个命名字段(不是id,因为每个字段的id应该是唯一的),注意名称而不是名称的使用:

@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...