2
I have a project in ASP.NET MVC 4
and in it I have a requisition Ajax
where calls the controller
sending only one parameter. The request does everything correctly, brings me a list with the data I need.
What I want to know is how can I use this list to write a code on Html
within the Success.
Or if I can get the answer from Ajax
and use the list in Html
outside the Success.
AJAX code:
<script type="text/javascript">
var item = "";
$.ajax({
type: 'POST',
url: "@Url.Action("_ListaHistorico", "Solicitacoes")",
data: { IdSolicitacao: id},
dataType: 'JSON',
success: function (result) {
for (var i = 0; i <= result.lenght; i++) {
}
},
error: function (msg) {
alert("ERRO!");
}
});
</script>
Controller Code:
[HttpPost]
public JsonResult _ListaHistorico(int IdSolicitacao)
{
List<List<string>> listaHistorico = new List<List<string>>();
foreach (
var historico in
context.Solicitacao.Include("Usuario")
.Where(x => x.IdSolicitacao == IdSolicitacao)
.FirstOrDefault()
.HistoricoSolicitacao.ToList())
{
listaHistorico.Add(new List<string>
{
historico.DataCadastro.Value.ToShortDateString(),
historico.Usuario.Nome,
historico.Observacao
});
}
return Json(listaHistorico, JsonRequestBehavior.AllowGet);
}
Just to warn you, you missed the entry on
result.lenght
the correct would beresult.lenght
, I’ve already edited. If you put it in the code this way, this would be a problem ^^– Paulo Roberto Rosa
@Pauloroberto I don’t know if this edition was a good idea, it seems unlikely that the author was writing the letter by letter, line by line the code to put it in the question... It is more likely that it was copy&paste and that the typo is being the problem / a problem he is having.
– Zuul
@Zuul you’re right, I went back.
– Paulo Roberto Rosa
The syntax was really wrong. Thank you for correcting.
– Thiago Alex