fea*_*net 24 asp.net-mvc jquery parameter-passing getjson url-parameters
我有以下我正在尝试将列表/数组发送到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');
});
Run Code Online (Sandbox Code Playgroud)
在我的控制器中:
public JsonResult UpdateStockList(int[] ids, bool[] stocked) { }
Run Code Online (Sandbox Code Playgroud)
两个参数都是空的.
请注意,如果我将参数更改为单个项目
params.ids = 1;
params.stocked = true;
public JsonResult UpdateStockList(int ids, bool stocked) { }
Run Code Online (Sandbox Code Playgroud)
然后它工作正常,所以我不认为这是一个路由问题.
Dar*_*rov 39
尝试设置traditional标志:
$.ajax({
url: '/home/UpdateStockList',
data: { ids: [1, 2, 3], stocked: [true, false] },
traditional: true,
success: function(result) {
alert(result.status);
}
});
Run Code Online (Sandbox Code Playgroud)
适用于:
public ActionResult UpdateStockList(int[] ids, bool[] stocked)
{
return Json(new { status = "OK" }, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
hip*_*ail 22
除了要求.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');
});
Run Code Online (Sandbox Code Playgroud)
不幸的是,虽然看起来jquery提供了一个"传统"标志来在jQuery.ajax上切换这种行为,但它并没有在jQuery.getJSON上.解决这个问题的一种方法是在全球范围内设置标志:
jQuery.ajaxSettings.traditional = true;
请参阅jQuery.param的文档:http://api.jquery.com/jQuery.param/ 另请参阅此更改的发行说明:http://jquery14.com/day-01/jquery-14(搜索'传统的")
| 归档时间: |
|
| 查看次数: |
39077 次 |
| 最近记录: |