使用Ajax将jquery序列化到控制器asp mvc 尽管客户端发送了值(jquery),但控制器仍接收到空值的问题
1)jQuery Cod
function saveParent() {
var parrentArry = [];
var formElement = document.querySelector("form");
var formData = new FormData(formElement);
var parent = {} ;
formData.forEach(function (value, key) {
parent[key] = value;
});
parrentArry.push({parent});
var data = JSON.stringify({
parent: parrentArry });
var dataType = 'application/json; charset=utf-8';
$("#result").html(data);
$.ajax({
url: '/Admission/_AddAdmission',
type: 'Post',
contentType: "application/json; charset=utf-8",
data: data,
success: function (result) {
$("#result").html(result);
}
});
}
控制器代码
public JsonResult _AddAdmission([FromBody] Parent[] parent ,string child)
{
foreach (var P in parent)
{
Parent pm = new Parent();
pm.Fname = P.Fname;
pm.Lname = P.Lname;
pm.Email = P.Email;
}
return new JsonResult();
}
//运行后获取值为空的父对象