Sending a json by Ajax and the controller receives all the data in null?

0

Sent some table of inputs with the following code:

        $('#guardar').click(function(){
            var header = $('table thead tr th').map(function () {
                return $(this).text().trim();
            });

            var Datos = $('table tbody tr').map(function (i) {
                var row = {};
                $(this).find('td').each(function (i) {
                    var rowName = header[i];
                    row[rowName] = $(this).find('input').val();
                });
                return row;
            }).get();

            $.ajax({                  
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                type: "POST",
                url: '@Url.Action("Guardar", "Inicio")',
                data: JSON.stringify({Datos}),
                dataType: JSON,
                success: function (data) {

                },
                error: function (r) {
                    alert("Error del servidor");
                }
            });
        });

and I receive it with the controller:

    public JsonResult Guardar(List<Temp> Datos)
    {
       object ver = Datos;

        return Json(Datos, JsonRequestBehavior.AllowGet);
    }

and I have the class:

    public class Temp
    {
        public String Albaran;
        public String Cp;
        public String Destinatario;
        public String Direccion;
        public String Fecha;
        public String Observaciones;
        public String Poblacion;
        public String Provincia;
        public String Telefono;

    }

Capture the json in the console.log:

The fact is that I receive all 20 rows in the jsonresult that I have but all their values in null.

Sold

I have to add {get; set} to the variables of the Temp class

    
asked by Acd 16.06.2017 в 20:42
source

2 answers

0

The problem was that I have to add the { get; set; } to the class% Temp .

    
answered by 21.06.2017 / 17:32
source
0

Good morning.

The easiest thing is that you first try to send an object in a "manual" way. Do not try to load the object from the form to do the test.

var Datos = {'Albaran': 'datos', ...};

If it works, check that the object that is being sent is correct from the JavaScript terminal (F12), simply with a: console.log (Data);

Just before your call to Ajax, to see that the object is correct. (I know you've pasted an image, but it gives me error 503 when trying to load it.)

Also comment that you do not have to pass the string to the object. It is possible that the browser or the application is taking the post directly as a text and not as the object you want to send.

It may also be easier to solve, that table with inputs, put it inside a form with an ID, and get an object directly with:

var Datos = $('#idFormulario').serializeArray();

Take the attribute "name" as the key, and the value of the field as a value.

If this does not work for you, copy an example of the table in html so we can do better tests.

    
answered by 16.06.2017 в 22:12