Send list of objects JSON c #

1

. However, in the controller, I only get the Field id, and nothing from Answer , I can not see the problem. My array can get the data as it should, just not everything is passed to the controller.

JSON

var urlResposta = '@Url.Action("Create", "Resultado")';
var dtoEnvio = new Array();
            $('.resposta').each(function () {
                var obj = {
                        Resposta: {
                            Resp: $(this).find('.perg-resp').first().val()
                        },
                        Campo: { Id: $(this).find('#hidID').first().val() }
                    };
                dtoEnvio.push(obj);
            });
            console.log(dtoEnvio)

        $.ajax({
            type: "POST",
            url: urlResposta,
            cache: false,
            data: JSON.stringify(dtoEnvio),
            datatype: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (result) {
            }
        })
    };

C # class properties

public class Resposta
    {
        public virtual Int64 Id { get; set; }
        public virtual String Resp { get; set; }
        public virtual Campos Campo { get; set; }
        public virtual Usuario Usuario { get; set; }
    }

Controller

[HttpPost]
        public JsonResult Create(List<Resposta> listResposta)
        {
        }

Response List DTO

    
asked by anonymous 29.11.2015 / 22:16

1 answer

0

The Json field names must match the object's property names, so the framework does the automatic conversion.

In the case of the question, Json has an Answer and the Controller waits for Answer.

Since it does not find a Response in the Controller then it ignores the Json field, and since it does not find Resp in the Json it does not load the Resp value of the Object.

    
30.11.2015 / 17:40