0
Good night, my friends! I have no programming experience, I appreciate any help.
I created a simple query to list the query in a table.
<script type="text/javascript">
$(document).ready(function () {
$("#btnPesquisarPosto").click(function () {
var termoPesquisa = $("#pesquisaPosto").val();
$.ajax({
method: "GET",
url: "/Posto/FiltrarPorNome?pesquisa=" + termoPesquisa,
success: function (data) {
console.log(data);
$("#tblPosto tbody > tr").remove();
$.each(data, function (i, posto) {
$("#tblPosto tbody").append(
"<tr class='animated fadeIn'>" +
" <td>" + posto.Ativo + "</td>" +
" <td>" + posto.Nome + "</td>" +
" <td>" + posto.CNPJ + "</td>" +
" <td>" + posto.InscricaoEstadual + "</td>" +
" <td>" + posto.DataCadastro + "</td>" +
" <td>" + posto.Telefone + "</td>" +
" <td>" +
" <a class='btn btn-sm btn-primary' title='Alterar' href='/Posto/Edit'" + posto.Id + "'><i class='zmdi zmdi-edit' aria-hidden='true'></i></a>" +
" <a class='btn btn-sm btn-success' title='Detalhes' href='/Posto/Details'" + posto.Id + "'><i class='zmdi zmdi-menu' aria-hidden='true'></i></a>" +
" <a class='btn btn-sm btn-danger' title='Exluir' href='/Posto/Delete'" + posto.Id + "'><i class='zmdi zmdi-close' aria-hidden='true'></i></a>" +
" </td>" +
"</tr>"
);
});
},
error: function (data) {
alert("Houve um erro na pesquisa :( ")
console.log(data);
}
});
});
});
</script>
I did the test with Postman and is returning the correct Json, according to the search, however, the table fills the fields with "Undefined". The method in my controller is not asynchronous.
Ha! I am using ASP.NET(full framework) - MVC 6 - EF7
Controller:
public ActionResult FiltrarPorNome(String pesquisa)
{
List<Posto> postos = _context.Posto.Where(a => a.Nome.Contains(pesquisa)).ToList();
List<PostoViewModel> PostoView = Mapper.Map<List<Posto>, List<PostoViewModel>>(postos);
return Json(PostoView);
}
What is the result of your
console.log(data);
?– fernandoandrade
I don’t know how to attach another image, even on another site. Follow the link; https://uploaddeimagens.com.br/imagens/erro_console-png-2
– João Chagas
You’re making the wrong reference, your object has the variables in minuscule and you’re trying to access
posto.Ativo
where the correct would beposto.ativo
– fernandoandrade