Send list of JSON c#objects

Asked

Viewed 972 times

1

Well, next, I’m trying to send a list of objects through JSON to a C#controller, where it contains an object Answer, and in that object, contains another call Field. However, in the controller, I only get the id of Field, and no Answer, I can’t see the problem. My array can take the data as it should, only it is not passed everything to 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) {
            }
        })
    };

Properties class C#

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 Lista de resposta DTO

DTO

  • 1

    Puts the Json that you are sending, will help understand the problem.

  • @That would be Ricardo ? 0: Object Field: Object Id: "1" proto: Object Answer: Object Resp: "a" proto: Object proto: Object Because the code of JSON and such, ta ai em cima.. thanks !

  • This, the same content of Json, edits the question and puts it formatted there. What are you using for same test.

  • In control you wait Resp, in Json sends Reply, if it has not mapped in hand it will not do automatically, changes Jason to Resp or the control to Reply, if it works tells me q create a response

  • @Ricardo, exactly that Ricardo, I removed Reply in json, and it worked, if you want to create the answer, it works like this: $('.resposta').each(function () {&#xA; var obj = {&#xA; Resp: $(this).find('.perg-resp').first().val(),&#xA; Campo: { Id: $(this).find('#hidID').first().val() }&#xA; };&#xA; dtoEnvio.push(obj);&#xA; });

  • I added the answer, if solved and you want, mark it .

Show 1 more comment

1 answer

0


The name of the Json fields must match the object properties names, this is how the framework does the automatic conversion.

In case of question Json has Answer and Controller expects Resp.

Since it does not locate Answer in the Controller then it ignores the Json field, and since it does not find Resp in Json it does not load the Resp value from the Object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.