Result of JSON on a table

5

Good afternoon,

And I put a search field, and once the search is done, a table with the found users pops up. The part of the query I was able to do, but now I'm not getting popular (actually I have no idea how does, I researched but anyway I could not) a table with the names ..

Follow the part of the controller (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);
    }

JS (I took this part of an example that 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 is working. The problem is in the hr of forming the same table .. could anyone tell me how to do this?

    
asked by anonymous 12.11.2015 / 22:33

1 answer

3

If the data in your query is returning well, then the problem seems to be in the form with looping of the data. the _each function judges to be of the _underscore technology. I think you should use $ .each:

$("#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);

    }
});
    
12.11.2015 / 23:04