Good evening, friends! I have no programming experience, thanks for any help.
I created a simple query to list the search 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, in the table fills the fields with "undefined". The method on my controller is not asynchronous.
Ha! I'm 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);
}