I have a project in ASP.NET MVC 4
and in it I have a Ajax
request where it calls the controller
sending only one parameter. The request does everything correctly, it brings me a list of the data I need.
What I want to know is how I can use this list to write code in Html
within success.
Or if I can get the answer from Ajax
and use the list in Html
out of 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);
}