I have a function in ajax that creates an arrangement of an object and sends it to the PageModel with a post.
function guardarOrden(data) {
return $.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: "/EntradasAlmacen/NuevaEntrada?handler=SaveOrder",
data: data,
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
success: function (result) {
alert(result);
location.reload();
},
error: function () {
alert("Error!")
}
});
}
$("#guardarOrden").click(function (e) {
e.preventDefault();
var ordenArr = [];
ordenArr.length = 0;
$.each($("#tblProductos tbody tr"), function () {
alert($(this).find('td:eq(1)').html());
ordenArr.push({
IdProducto: $(this).find('td:eq(0)').html(),
DescProducto: $(this).find('td:eq(1)').html(),
Cantidad: $(this).find('td:eq(2)').html()
});
});
var data = JSON.stringify({
order: ordenArr
});
$.when(guardarOrden(data)).then(function (response) {
alert(response);
console.log("Respuesta:"+response);
}).fail(function (err) {
console.log(err);
});
});
But when you call the method the value always arrives null to the PageModel, which may be failing.
This is the function of the pagemodel
[HttpPost]
public async Task<IActionResult> OnPostSaveOrderAsync(Producto[] order)
{
}