JSON output in a table

Asked

Viewed 354 times

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.?

1 answer

3

If your query data is returning well, then the problem seems to be in the way you loop the data. the _each function I think is 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);

    }
});
  • Good morning, I tried to do what you suggested, but it didn’t help. When I search it displays the error: "Error[Object Object]", this only when it finds some result, when it does not find, nor does the message that it was not found register display.. And about _each, it’s the _underscore technology yes, I left it that way because in the example I saw it was that way.. Thanks for trying to help :D..

  • Please give an example of the json data that is expected to better understand

  • {"name":"Igor Marcante", "razaosocial":"Igor", "cpf_cnpj": "21574898521"}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.