AJAX function to update table

Asked

Viewed 457 times

0

I have this function in AJAX to update the data of a table, the data is coming from the controller correctly, but it does not update with the correct data, it updates blank.

function buscaFornecedores(id) {
var url = "/Produto/BuscaFornecedor";
$.ajax({
    url: url,
    type: 'GET',
    data: { id: id},
    success: function (data) {
        $("#tabelaf").html(data);
    }
});

}

It would be possible to do something like this link ? Utilizo MVC Core Page Razor.

EDIT

I receive this way the data, giving a console.log(date.)

inserir a descrição da imagem aqui

EDIT This is my HTML:

<table class="table table-responsive table-hover" id="tabelaf">
                    <thead>
                        <tr>
                            <th>Fornecedores</th>
                            <th style="text-align:right"><a data-toggle="modal" data-target="#myModalAdd" title="Adicionar Novo Fornecedor" class="btn btn-info"><i class="fa fa-plus"></i></a></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model.ProdutosFornecedores)
                        {
                            <tr class="tr">
                                <td>@item.FornecedorProduto.Nome</td>
                                <td align="right">
                                    <a class="link-excluir" href="#" data-id="@item.Id" title="Excluir"><i class="fa fa-trash-o fa-lg"></i></a>&nbsp;
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
  • What is the format of your answer ?

  • I updated the question.

  • The link you posted exemplifies exactly what you need. Did you consider using it that way ? You made a mistake ?

1 answer

1


Try this:

success: function (data) {
        var trHTML = '';
        $.each(data, function (i, item) {
            trHTML += '<tr><td>' + item.id+ '</td><td>' + item.fornecedorProduto+ '</td><td>' + item.fornecedorID + '</td><td>' + item.produtoFornecedor+ '</td><td>' + item.produtoID+ '</td></tr>';
        });
        $('#tabelaf').append(trHTML);
    }
  • I did so, and in the table appeared Undefined, in the fields, in case I use only id in this table, and in the viewmodel, I declare the tables to fetch the name, is that something with that ? Why didn’t the correct noem appear? I put my HTML code so you can understand. Thank you.

  • I put in the console.log(item. (field). And all comes undenifed.

Browser other questions tagged

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