How can I send an Arrangement in the post using JSON?

0

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)
    {
    }
    
asked by M. Brandle 26.06.2018 в 22:03
source

2 answers

0

Maybe you need to put [WebMethod] and change order by data:

[HttpPost] [WebMethod]     public async Task OnPostSaveOrderAsync (Product [] data)     {     }

    
answered by 29.06.2018 / 03:19
source
0

If you define the dataType: 'json' it is not necessary to perform the JSON.stringify

Web API With AJAX : Understanding POST Request in Web API

As you will see in the example, an object is sent directly

 var person = new Object();
 person.name = $('#name').val();
 person.surname = $('#surname').val();

 $.ajax({
     url: 'http://localhost:3413/api/person',
     type: 'POST',
     dataType: 'json',
     data: person,
     success: function (data, textStatus, xhr) {
         console.log(data);
     },
     error: function (xhr, textStatus, errorThrown) {
         console.log('Error in Operation');
     }
 });

try first by sending a simple command to see if it solves, then send the array that you could also define as List<Producto>

If you do not want to use the Object you could use the json notation

 var person = {
  name: $('#name').val(),
  surname: $('#surname').val()
 };

but do not apply the stringify

On the other hand, I see that the url says:

url: "/EntradasAlmacen/NuevaEntrada?handler=SaveOrder",

but in the api the name does not match OnPostSaveOrderAsync

So the action is called NuevaEntrada or OnPostSaveOrderAsync ? try to invoke the api using some application such as Postman

This taking into account that the name of the controller: EntradasAlmacen is correct

    
answered by 26.06.2018 в 23:33