【发布时间】:2018-05-31 09:49:01
【问题描述】:
我正在尝试使用 jQuery Ajax 帖子将对象数组和 int 传递到 Core MVC 控制器。
我已经看到了很多很多关于这个问题的答案,但我没有找到适合我的答案......似乎使用 Core MVC 不像经典 MVC 那样工作。
对象类:
public class Field : Nameable //herit of the ID and Name properties
{
public string Value { get; set; }
public Field () { }
}
MVC 控制器功能:
[HttpPost]
public JsonResult SaveFields(List<Field> fields, int id)
{
return Json(new { success = true, responseText = "OK" });
}
jQuery 函数:
$('#btnSaveFields').click(function () {
var $rows = $('#table').find('tr:not(:hidden)');
var data = [];
var id = 1;
// Turn all existing rows into a loopable array
$rows.each(function () {
var $td = $(this).find('td');
var obj = new Object();
obj.ID = $td.eq(0).children().attr('value');
obj.Name = $td.eq(0).text().trim();
obj.Value= $td.eq(1).text();
data.push(obj);
});
data.shift(); // don't take the headers row
console.log(data[0]); //seems ok
var fields = JSON.stringify({ 'fields': data });
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: '@Url.Action("SaveFields")',
//traditional: true, //don't work or wrong use ?
type: 'POST',
data: {
'fields': fields,
'id' : id
},
success: function (response) {
console.log('ok')
},
error: {
console.log('nok')
}
});
});
MVC 控制器函数调用有效,但参数为空。你知道我做错了什么吗?
【问题讨论】:
标签: jquery ajax asp.net-core asp.net-core-mvc