Error listing Data via Json [duplicate]

2

I am using a method to return an object of type JSON but it is not listing the information:

My Controller

   public ActionResult GetDados()
    {
        int codigoVenda = 2;

        try
        {
            SistemaDBEntities db = new SistemaDBEntities();

                List<ItensVenda> itensVenda = new List<ItensVenda>();
                itensVenda = db.ItensVenda.Where(s => s.CodigoVenda == codigoVenda).ToList();

                //var venda = db.Venda.Where(s => s.Codigo == codigoVenda).ToList();

                return Json(itensVenda, JsonRequestBehavior.AllowGet);

        }
        catch (Exception)
        {

            throw;
        }
    }

My Script

 $(document).ready(function () {

    $.ajax({           
        type: "GET",
        url: "/Venda/GetDados",
        success: function (itensVenda) {

            if (dados != null) {

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

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

                    var tbody = $('#tbody');
                    var tr = "<tr>";
                    tr += "<td>" + itensVenda[i].CodigoProduto;
                    tr += "<td>" + itensVenda[i].Quantidade;
                    tr += "<td>" + itensVenda[i].PrecoUnitario;

                    tr += "<td>" + "<button class='btn btn-info' onclick=Editar(" + itensVenda[i].Id + ")>" + "Editar";
                    tr += "<td>" + "<button class='btn btn-danger' onclick=Deletar(" + itensVenda[i].Id + ")>" + "Deletar";

                    tbody.append(tr);



                });
            }
        }
    });
});

I'm pretty sure he does not list because of this error:

Does anyone know what's wrong?

    
asked by anonymous 13.10.2015 / 22:01

1 answer

1

The ProxyCreationEnabled is enabled, put a line so it will work:

db.Configuration.ProxyCreationEnabled = false;

Total example:

public ActionResult GetDados()
{
    int codigoVenda = 2;

    try
    {
        SistemaDBEntities db = new SistemaDBEntities();     

        db.Configuration.ProxyCreationEnabled = false;

        List<ItensVenda> itensVenda = db.ItensVenda.Where(s => s.CodigoVenda == codigoVenda).ToList();      

        return Json(itensVenda, JsonRequestBehavior.AllowGet);

    }
    catch (Exception)
    {

        throw;
    }
}

I would even add this:

db.Configuration.LazyLoadingEnabled = false;

To not load the relationship data (Good practice) and if you need some relationship use Include.

    
13.10.2015 / 22:16