1

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.

2
  • "I don't know if the Ajax is wrong" Try making a raw HTTP request (with e.g. cURL, PostMan, or whatnot) without any JavaScript. If it works, your problem is with the JavaScript. If not, the problem is server-side. Commented Feb 16, 2024 at 6:23
  • If you follow the github link you have shared and it works for me. I could get the value from the oFormPedidoVM. Result like this, I suggest you should recheck the url, controller or action is right and the model is right or not. Commented Feb 16, 2024 at 8:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.