1
Good afternoon to all,
I have a problem which is as follows. Through an Ajax request I return a list of objects. This class of mine is just to load the data I need to send to the view that makes the Procedure call in the controller so it is not declared as Dbset in the context class.
When I did the code described below Asp.net mvc 6 worked, but when I put the same code in Asp.net core gave error, it seems that I can’t access the properties. ex: alert(response.result[i].Correto)
appears 'Undefined', and if put only alert(response.result)
appears Object object
. But the amount of registrations you have on the list appears right (response.result.length
). Someone can help me?
Disregard the ' ' ' after the minor signal, I did it to make the code complete.
Below is the class, the controller action and the jquery code with the ajax request:
public class RespostaProvaModel
{
public int QuestaoId { get; set; }
public string TextoPergunta { get; set; }
public string TextoResposta { get; set; }
public bool Correto { get; set; }
}
[HttpPost]
public ActionResult <List<RespostaProvaModel>> CarregaProva(List<RespostaProvaModel> resultadoProva)
{
List<RespostaProvaModel> resultadoFinal = new List<RespostaProvaModel>();
foreach (RespostaProvaModel resposta in resultadoProva)
{
RespostaProvaModel resultado = _educ365Context.Respostas.Where(r => r.QuestaoId == resposta.QuestaoId)
.Select(r => new RespostaProvaModel
{
QuestaoId = r.QuestaoId,
TextoResposta = r.RespostaDescricao,
Correto = (resposta.TextoResposta.ToLower().Equals(r.RespostaDescricao.ToLower()))
}).FirstOrDefault();
resultadoFinal.Add(resultado);
}
return Json(new { result = resultadoFinal.ToList() });
}
Ajax excerpt in view
$.ajax({
type: 'POST',
url: '@Url.Action("CarregaProva","Prova")',
data: { resultadoProva },
success: function (response) {
alert("Tamanho: " + response.result.length);
for (var i = 0; i < response.result.length;i++) {
alert("Correto: " + response.result[i]);
}
Alert does not display object contents. Use
console.log
to see the structure of the data. There you can analyze better.– Sam
Sam, I used the console.log and it came up the same thing. I put Response.result[i]. Correct, it should appear true but it appeared unfefined
– Marcelo
Change
response.result[i].Correto
forJSON.stringify(response.result[i])
, appears the correct json?– Costamilam