Return list to ajax

Asked

Viewed 272 times

0

I have this function to load some data, but the need to load a list and include in a table the function was thus:

[HttpPost]
    public ActionResult CarregaProduto(string id)
    {
        try
        {
            var item = db.Produtos.Where(r => r.Codigo == id).Single();
            var produtoempresa = db.ProdutosEmpresas.Include(a => a.EmpresaProduto).Where(a => a.ProdutoEmpresa.Codigo == id).ToList();
            return Json(new
            {
                nomeProduto = item.nome,
                precoUnitario = item.PrecoCusto,
                id = item.Id,
                tipo = item.TipoProduto,
                Resultado = item.Codigo,
                listaEstoque = produtoempresa,
            });

        }
        catch { return Json(new { Resultado = 0 }); }
    }

To produtoempresa returns to list, but how can I send the data and upload it, via HTML ?

Here is the function:

function CarregaProduto(id) {
    var url = "/PedidoVenda/CarregaProduto";

    $.ajax({
        url: url
        , data: { id: id }
        , type: "POST"
        , datatype: "html"
        , success: function (data) {
            //console.log(data.resultado);
            if (data.resultado != 0) {
                $("#descricaoproduto").html(data.nomeProduto);
                var preco = ((data.precoUnitario));
                $("#precocusto").val(parseFloat(preco).toFixed(2).replace(".", ","));
                $("#produtoid").val(data.id);
                $("#idproduto").val(data.resultado);
                $("#tipo").val(data.tipo);
            }
            else {
                $("#descricaoproduto").html("Não encontrado, pesquise novamente.");
                $("#precocusto").val("");
                $("#idproduto").val("");
            }
        }
    });
}

How can I load the list in this function CarregaProduto ? I managed to send, put one console.log(data.listaEstoque); it is returning the data correctly: inserir a descrição da imagem aqui

I tried to add in a div, this way:

 var dvItems = $("#dvItems");
                dvItems.empty();
                $.each(data, function (i, item) {
                    var $tr = $('<li>').append(data.listaEstoque).appendTo(dvItems);
                });

But it doesn’t work either, how to load the data ?

  • 1

    Ta, I don’t understand your doubt, but first you should be returning a Jsonresult and not an Actionresult

  • In html you can receive this data via ajax and le them

  • @Marcosbrinner I need to carry one table, i am using MVC CORE

  • @Marcosbrinner I need to send beyond these data that are in the Json Return, I need to return now too list

  • @Marcosbrinner edited the question, now I think it’s clearer, I just need to load the data in html, I tried to load a div, but it didn’t work.

1 answer

1


I don’t quite understand your doubt. But, come on:

  • Return a Jsonresult;
  • Browse the product and add to the list that will return in JSON;

Below was a solution I used here in the company to use Googlechart returning a Json:

    [WebMethod]
    public JsonResult Filtro(string dataInicio, string dataFinal, int? searchContrato)
    {
        try
        {

            int qtdeAjuda = QuantidadeAjuda(dataInicio, dataFinal, searchContrato);

            List<Analytics> g = new List<Analytics>()
            {  
                new Analytics(13, "Ajuda" + "(" + qtdeAjuda + ")", qtdeAjuda ),

            };

            return Json(new { JSONList = g }, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            return Json(null, JsonRequestBehavior.AllowGet);                
        }            
    }

See if this can help you in relation to your doubt. Below is just a return example to give you a north in solving your problem.

<script>
$(function () {
    $.ajax({
        dataType: "json",
        type: "GET",
        url: "/Home/GetDados",
        success: function (dados) {
            $(dados).each(function (i) {
                document.writeln("<p>Nome: " + dados[i].Nome + " | URL: " + dados[i].URL + "</p>")
            });
        }
    });
});

  • Unused asp.net core I don’t have the option to use JsonRequestBehavior in case your solution doesn’t work for me.

  • I edited the question, I just need to pass the data to html, the way I’m doing is not working.

Browser other questions tagged

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