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)
{
}
Puts the Json that you are sending, will help understand the problem.
– Ricardo
@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 !
– Caio Cesar
This, the same content of Json, edits the question and puts it formatted there. What are you using for same test.
– Ricardo
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
@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 () {
 var obj = {
 Resp: $(this).find('.perg-resp').first().val(),
 Campo: { Id: $(this).find('#hidID').first().val() }
 };
 dtoEnvio.push(obj);
 });
– Caio Cesar
I added the answer, if solved and you want, mark it .
– Ricardo