And Lozano
And Lozano

Reputation: 11

FromBody null in Master Detail Create ASP.NET

I need your help, I was trying a master detail create. I followed this project: https://github.com/CodigoEstudiante/067_ProyectoEFMaestroDetalleNet6/blob/master/MaestroDetalle/Controllers/HomeController.cs

In the video works but not for me, crashes and shows that oFormPedidoVM is null. I don't know if the Ajax is wrong for the version or what. Print statement before Ajax shows that there is an object.

View:

console.log(oFormPedidoVM);

jQuery.ajax({
            url: "@Url.Action("Index", "FormPedidoVM")",
            type: "POST",
            data: JSON.stringify(oFormPedidoVM),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: (data) => {
                alert("Pedido enviado");
                console.log(data);
                 if (data.respuesta) {
                    alert("Pedido creado");
                }
            },
});

Controller:

[HttpPost]
public IActionResult Index([FromBody] FormPedidoVM oFormPedidoVM)
{
    Console.WriteLine(oFormPedidoVM.ToString());

    FormPedido oFormPedido = oFormPedidoVM.oFormPedido;
    oFormPedido.DetalleFormPedidos = oFormPedidoVM.oDetalleFormPedido;

    _context.FormPedidos.Add(oFormPedido);
    _context.SaveChanges();

    return Json(new { respuesta = true });
}

My repo: https://github.com/Bubbl33s/maki-yumpu-c4

Tried with different controllers and views, changing the behavior of [FromBody], multiple print statements.

Upvotes: 1

Views: 43

Answers (0)

Related Questions