Load table via AJAX

Asked

Viewed 437 times

1

I’m trying to load the data from a table, via ajax, this way:

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

And here’s how it’s in the controller:

  [HttpGet]
    public ActionResult BuscaFornecedor(int id)
    {
        var fornecedor_produto = db.ProdutosFornecedores.Where(p => p.ProdutoID == id).ToList();

        return Json(new { Resultado = fornecedor_produto });
    }

But it appears blank, what is missing in the code ?

Edit: The log console appears this way: inserir a descrição da imagem aqui

This is the code of the table:

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

I tried to use it, doing it this way:

 public JsonResult BuscaFornecedor(int id)
{
    var fornecedor_produto = db.ProdutosFornecedores.Where(p => p.ProdutoID == id).ToList();

    return Json(new { Resultado = fornecedor_produto });
}

But it didn’t work either. In common MVC it would use Jsonrequestbehavior.Allowget, but in MVC Core it is not possible.

EDIT:

I tried to pass this way too, but still the same problem:

public ActionResult BuscaFornecedor(int id)
    {
        var fornecedor_produto = db.ProdutosFornecedores.Where(p => p.ProdutoID == id).ToList();

        return new JsonResult(new { Resultado = fornecedor_produto });
    }
  • $("#tabelaf").html(data.resultado);?

  • Keep coming in white

  • Is your (product vendor) being populated? If yes use: Return Json(vendor_product, Jsonrequestbehavior.Allowget);

  • put a console.log(data) within the function success to see in the browser console the data returned and how is the structure of the information for you to access.

  • @Netinhosantos did not work.

  • @Thiagomagalhães made the console.log, and put the image with the returned data in the question.

  • What exactly is your problem.? When you see the Edit of your question the data is being returned in ajax.

  • in that #tabelaf you want to return a table?

  • I put the code of the table so that you can understand. This @Barbetta want to return to her the data from ajax.

  • First, if you look at your JSON, you will see that it is coming null in various fields. Second, either you use Razor, or Ajax on your table, the two things will get overhead. Third, you are receiving json and not html, use a for in javascript so that you print a table row for each item of the received json.

  • I just need the data provider id and product id. the other fields will be removed, I believe the problem is not this

  • @Gustavosantos you have some example to help me ?

Show 7 more comments

1 answer

1


Turns out you’re returning a Json not the table html.

Change your code as follows:

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


[HttpGet]
public ActionResult BuscaFornecedor(int id)
{
    var fornecedor_produto = db.ProdutosFornecedores.Where(p => p.ProdutoID == id).ToList();

    return PartialView("_SuaPartialView", fornecedor_produto);
}

Now will return the html with the information

An alternative with less code to load the information is to do so:

function buscaFornecedores(id) {
    $("#tabelaf").load("/Produto/BuscaFornecedor/" + id, function(){
        $(".modal").modal('show');
    });     
}
  • I just didn’t understand the part of Partialview @Barbetta, as I will link the table to partialview ?

  • In the part you added the code of the table that should be a PartialView, she has nothing other than a View, what changes is that when you return it your application will not go through ViewStart. In his View she has a model, on the part of PartialView("_SuaPartialView", fornecedor_produto); you are indicating the name of this View and the return model side

  • In the _SuaPartialView rename the file name cshtml that you want to load, it is important that the model passed beside is the mesmo that View receives

  • this code is inside a modal, I’m not knowing how to handle Partialview, I’m using mvc core.

  • I edited the answer, I put to after load the information he open the modal

  • in ordinary mvc, it works, I did the test, but I can’t make it work in mvc core. :(

Show 1 more comment

Browser other questions tagged

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