Error while listing Data via Json

Asked

Viewed 1,603 times

2

I’m wearing a method to return an object of the type JSON but is not listing the information:

Man 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;
        }
    }

Man 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 doesn’t list because of that mistake: inserir a descrição da imagem aqui

Someone knows what’s wrong?

  • I believe that this is not the best approach to what search, I would use partialView to fill the table. But, try to access the url localhost:63729/sale/Getdados and post the error that is appearing

  • This error appears: A circular reference was detected when serializing an object of type 'System.Data.Entity.DynamicProxies.Venda_9f946f25ade24f28cdca309e65a9e7ece50af4c1b01232ed376a7b4dcf436e82'.

  • 1

    Take a look in this answer, that you can solve your problem. And edit your question and add the error in it, it is easier to understand your real problem.

  • Actually there is a bit of difference one is Webforms the other is Aspnet MVC and besides db.Configuration.ProxyCreationEnabled = false; (Entityframework configuration, which is recommended to take) avoids giving the error and solves the problem without having to make another class and configuration. Of course the proposed response is also a solution, but at another time in another case I believe it would be legal to have the solutions for both! I don’t think DUPLICATE!

1 answer

0


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

Even I’d put that one on:

db.Configuration.LazyLoadingEnabled = false;

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

Observing: really the tip proposed by the OP in the comments is the best work with PartialView.

  • 1

    Congratulations @Cezar, thanks, now it worked... Thank you very much!

  • after using the Configuration you need to return it to normal for the other controllers? or this config is only in the inserted action?

Browser other questions tagged

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