Error returning JsonResult

1

I'm having trouble understanding this:

When I click on the Datatable line to edit the Client data, an error occurs whenever the Client has a Contact or a Registered Address and this is very common.

This is code that is triggered when I double click on the Datatable line:

VIEW

        // BUSCA DETALHES DO CLIENTE
        $.ajax({
            url: "ObterClientePorId",
            type: "post",
            datatype: "json",
            contentType: "application/json charset=uft-8",
            data: JSON.stringify({ "id": id }),
            success: function (data) {

                if (data != null) {
                    alert("Nome: " + data.Result.NMCLIENTE);
                    var url = "Create?id=" + id;
                    window.location.href = url;
                }

            },
            error: function (xhr, err) {
                alert(err.message);
            }
        });

Controller:

 public JsonResult ObterClientePorId(int id)
    {
        var ocliente = _IRepositorio.ListarClientePorId(id);
        return Json(new { Result = ocliente }, JsonRequestBehavior.AllowGet);
    }

Repository:

public TBCliente ListarClientePorId(int? id)
    {
        return _repositorio.Clientes.FirstOrDefault(c => c.TBCLIENTEID == id);

    }

EXECUTION PRINT:

View:

Controller:

Repository:

Controller

NotethattheClienthasaRegisteredAddress

View:

Heretheerroroccurs,alwayswhentheClienthasanAddressoraContactthatiscommon:

Thankyou!

**Doubleclickerror**

    
asked by anonymous 23.09.2015 / 14:39

1 answer

3

You have a circular reference problem. The JSON serializer attempts to serialize an entity A, which refers to an entity B, and which by some path references A again.

There is already an answer for this here .

Additionally, it is best not to use the repository approach when using the Entity Framework. It does not bring any advantage and still brings you problems.

    
23.09.2015 / 17:15