How to insert information into a Json object?

0

My problem is similar to this question: Question Stackoverflow

But in this case the Json object is used to populate 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 the listing listed the product code, but in fact you need to enter the description for that product, not the code.

You are giving the following error: A circular reference was detected when serializing an object of type 'CommercialSystem.Models.ItemsSale'.     

asked by anonymous 06.12.2015 / 19:31

1 answer

1

About the circular serialization error, implement the content of this answer to solve .

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 it needs to.

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>
    
06.12.2015 / 19:46