使用NickW的建议,我可以用things = JSON.stringify({ 'things': things });这是完整的代码。$(document).ready(function () {
var things = [
{ id: 1, color: 'yellow' },
{ id: 2, color: 'blue' },
{ id: 3, color: 'red' }
];
things = JSON.stringify({ 'things': things });
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: '/Home/PassThings',
data: things,
success: function () {
$('#result').html('"PassThings()" successfully called.');
},
failure: function (response) {
$('#result').html(response);
}
}); });public void PassThings(List things){
var t = things;}public class Thing{
public int Id { get; set; }
public string Color { get; set; }}
我从中学到了两件事:在Ajax()函数中,contentType和dataType设置是绝对必要的。如果他们失踪了就没用了。经过多次反复试验,我发现了这一点。
要将对象数组传递给MVC控制器方法,只需使用JSON.strgify({‘Things’:Things})格式即可。
我希望这能帮到别人!