How to insert information into a Json object?

Asked

Viewed 160 times

0

My problem is similar to that question: Question Stackoverflow

But in this case the object is used Json to popular the table.

Controller

 public ActionResult GetDadosItensVenda(int? Codigo)
    {
        try
        {
            db.Configuration.ProxyCreationEnabled = false;
            db.Configuration.LazyLoadingEnabled = false;

            List<ItensVenda> itensVenda = new List<ItensVenda>();

            itensVenda = db.ItensVenda.Include(s => s.Produto).Where(s => s.CodigoVenda == Codigo && s.Ativo == true).ToList();
            return Json(itensVenda, JsonRequestBehavior.AllowGet);

        }
        catch (Exception)
        {
            throw;
        }
    }

Script

<script>
$(document).ready(function () {
    var CodigoVenda = @ViewBag.CodigoVenda;
    $.ajax({
        type: "GET",
        url: "/Venda/GetDadosItensVenda?Codigo="+ CodigoVenda,
        success: function (itensVenda) {

            if (itensVenda != null) {
                var total = 0;

                $('#tbody').children().remove();

                $(itensVenda).each(function (i) {

                    total += (itensVenda[i].PrecoUnitario * itensVenda[i].Quantidade);

                    var tbody = $('#tbody');
                    var tr = "<tr>";
                    tr +=
                    tr += "<td>" + itensVenda[i].Codigo;
                    tr += "<td>" + itensVenda[i].CodigoProduto;
                    tr += "<td>" + itensVenda[i].Quantidade;
                    tr += "<td>" + itensVenda[i].PrecoUnitario;
                    tr += "<td>" + (itensVenda[i].PrecoUnitario * itensVenda[i].Quantidade);
                    tbody.append(tr);
                });
            }
            $("#Total").html("<p>"+ total + "</p>");
        }
    });
});

This listing worked normally, but in this listing appeared the code of the product, but in fact I need to inform the description of this product and not the code.

You’re making the following mistake: A circular reference was detected when serializing an object of type 'Systemscommercial.Models.Itensvenda'.

  • Where is the product description?

  • In the table Product. I put .Include(s => s.Produto) to do the JOIN.

1 answer

0


On the circular serialization error, implement the content of this response to resolve.

And then:

public ActionResult GetDadosItensVenda(int? Codigo)
{
        db.Configuration.ProxyCreationEnabled = false;
        db.Configuration.LazyLoadingEnabled = false;

        List<ItensVenda> itensVenda = new List<ItensVenda>();

        itensVenda = db.ItensVenda.Include(s => s.Produto).Where(s => s.CodigoVenda == Codigo && s.Ativo == true).ToList();
        return Json(itensVenda.Select(i => new {
            i.Codigo,
            i.CodigoProduto,
            i.Quantidade,
            i.PrecoUnitario,
            Produto = i.Produto.Nome
        }), JsonRequestBehavior.AllowGet);
}

Do not use try... catch in Controller. ASP.NET MVC already treats exception the way you need it.

View:

<script>
    $(document).ready(function () {
        var CodigoVenda = @ViewBag.CodigoVenda;
        $.ajax({
            type: "GET",
            url: "/Venda/GetDadosItensVenda?Codigo="+ CodigoVenda,
            success: function (itensVenda) {

                if (itensVenda != null) {
                    var total = 0;

                    $('#tbody').children().remove();

                    $(itensVenda).each(function (i) {

                        total += (itensVenda[i].PrecoUnitario * itensVenda[i].Quantidade);

                        var tbody = $('#tbody');
                        var tr = "<tr>";
                        tr +=
                        tr += "<td>" + itensVenda[i].Codigo + "</td>";
                        tr += "<td>" + itensVenda[i].CodigoProduto + "</td>";
                        tr += "<td>" + itensVenda[i].Produto + "</td>";
                        tr += "<td>" + itensVenda[i].Quantidade + "</td>";
                        tr += "<td>" + itensVenda[i].PrecoUnitario + "</td>";
                        tr += "<td>" + (itensVenda[i].PrecoUnitario * itensVenda[i].Quantidade) + "</td>";
                        tbody.append(tr);
                    });
                }
                $("#Total").html("<p>"+ total + "</p>");
            }
        });
    });
</script>

Browser other questions tagged

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