5
Good afternoon,
And I put a search field, and as soon as the search is done, I popular a table with the users found.. The part of the query I managed to do, but now I’m not getting popular(actually I have no idea how it does, I searched but still could not) a table with the names..
Follow the controller part (the one responsible for receiving the parameter, doing the search, and returning the values)
public JsonResult PorcurarPessoas (string searchString)
{
IQueryable<Pessoa> pessoas = db.Pessoa.Where(c => c.deleted_at == null);
if (!String.IsNullOrEmpty(searchString))
{
int codigo;
int.TryParse(searchString, out codigo);
pessoas = pessoas.Where(s =>
s.nome.ToUpper().Contains(searchString.ToUpper()) ||
s.id == codigo ||
s.Juridica.CNPJ.Contains(searchString) ||
s.Fisica.CPF.Contains(searchString) ||
s.nome_fantasia.ToUpper().Contains(searchString.ToUpper())
);
}
return Json(pessoas, JsonRequestBehavior.AllowGet);
}
J(I took this part from an example I found on the net..)
$("#procurar").on("click", function () {
$.ajax({
type: 'POST',
url: '@Url.Action("PorcurarPessoas", "Movimentacao")',
data: {
searchString: $("#searchString").val()
},
dataType: 'json',
success: function (data) {
debugger;
if (!data) {
alert('Pessoa não encontrada. Por favor verifiquei e tente novamente.');
} else {
var tabela = $("#datagrid");
var rows = "";
tabela.find("tbody td").remove();
_.each(data, function (item) {
rows += "<tr>";
rows += " <td>" + item.nome + "</td>";
rows += " <td>" + item.razaosocial + "</td>";
rows += " <td>" + item.cpf_cnpj + "</td>";
rows += "</tr>";
});
tabela.find("tbody").html(rows);
}
},
error: function (data) {
debugger;
alert('Error'+data);
}
});
CSHTML:
<table id="datagrid"></table>
I put a breakpoint in the query and it’s working. The problem ta na hr of forming the same table.. someone would know me how to do that.?
Have you tried debugging JS in your favorite browser? See here how to do.
– Leonel Sanches da Silva