C # WebMethod - Send and Receive the same object (custom) via Parameter

1

My code:

Object:

public class Person
{

    private string _Nome;
    private DateTime _Nascimento;

    public string Nome { get { return _Nome; } }
    public DateTime Nascimento { get { return _Nascimento; } }

    public Person(string Nome, DateTime Nascimento)
    {
        _Nome = Nome;
        _Nascimento = Nascimento;
    }

}

My Page (WebMethods):

[WebMethod]
public static Person SendPerson()
{
    return new Person("Jhon Snow", DateTime.Now);
}

[WebMethod]
public static string ReceivePerson(Person oPerson)
{
    return "OK!";
}

My Javascript:

var Person;
GetPerson();
SendPersonBack();
function GetPerson()
{
    $.ajax({
        type: "POST",
        url: "frmVenda.aspx/SendPerson",
        data: {},
        contentType: "application/json; charset=utf-8",
        success: function (RequestReturn) {
            Person = RequestReturn.d;
            console.log(Person);
        },
        error: function (error) {
            alert(error.statusText);
        }
    });
}
function SendPersonBack()
{
    $.ajax({
        type: "POST",
        url: "frmVenda.aspx/ReceivePerson",
        data: JSON.stringify({"oPerson": Person}),
        contentType: "application/json; charset=utf-8",
        success: function (RequestReturn) {
            alert(RequestReturn.d);
        },
        error: function (error) {
            alert(error.statusText);
        }
    });
}

The object is normally sent to clientside, but receiving it back on the server does not work! the object is the same as the one I sent, they are the same properties.

Why can not I get it back? Where is the problem?

    
asked by anonymous 11.06.2015 / 16:56

2 answers

1

I solved the problem by creating a parameterless constructor, setting all properties to string and adding the set method to all properties.

This caused .NET to recognize the object coming via ajax and populate the properties normally without errors.

public class Person
    {

        private string _Nome;
        private string _Nascimento;

        public string Nome { get { return _Nome; } set { _Nome = value; } }
        public string Nascimento { get { return _Nascimento; } set { _Nascimento= value; } }

        public Person()
        {

        }

        public Person(string Nome, DateTime Nascimento)
        {
            _Nome = Nome;
            _Nascimento = Nascimento.ToString();
        }

    }
    
11.06.2015 / 22:29
0

Dude, I think you should serialize. The problem that what your jquery sends back may be getting lost. I work in a similar way, but I use serialization to do it. See below an example of mine. In my controller I have a method that executes a LINQ. The result of this LINQ, I serialize and submit to my jquery function. See the controller: Note that I return a JSon.

[HttpPost]
        public JsonResult PreencheCargo(int _resp)
        {
            RupturaEntities db = new RupturaEntities();

            var result_responsavel = db.Responsavel
                .Where(w => w.IDResponsavel == _resp)
                .Join(db.Nivel_Responsavel, r => r.IDNivel_Responsavel, n => n.IDNivel_Responsavel, (r, n) => new { r, n })
                .Join(db.Responsavel_Motivo, rm => rm.r.IDResponsavel, m => m.IDResponsavel, (rm, m) => new { rm, m })
                .Join(db.Responsavel_Unidade_Negocio, rn => rn.rm.r.IDResponsavel, un => un.IDResponsavel, (rn, un) => new { rn, un })
                .Join(db.Responsavel_TipoPDV, rtp => rtp.rn.rm.r.IDResponsavel, tp => tp.IDResponsavel, (rtp, tp) => new { rtp, tp})
                .Join(db.TipoPDV, tpdv => tpdv.tp.IDTipoPDV, tdv => tdv.IDTipoPDV, (tpdv, tdv) => new { tpdv, tdv})
                .Select(s => new { s.tpdv.rtp.rn.rm.r.IDResponsavel, s.tpdv.rtp.rn.rm.r.NM_Responsavel, s.tpdv.rtp.rn.rm.r.NM_Responsavel_Sigla, 
                                   s.tpdv.rtp.rn.rm.r.IDNivel_Responsavel,s.tpdv.rtp.un.Unidade_Negocio, s.tpdv.rtp.rn.m.Motivo, s.tdv.IDTipoPDV, s.tdv.Descricao }).ToList();

            return Json(new { result_responsavel }, JsonRequestBehavior.AllowGet);
        }

That way I now have an object on the other side, bit by bit, that jquery will deserealize and work with the rebuilt object. My jquery function returns to my method in the same way, but I set a parameter to pass to the controller. I think that's not a problem, but I do it with myself. See below my jquery.

function CarregaDadosCargo(ajaxParameter) {

    $.ajax({

        url: '/CadastroCargo/PreencheCargo',
        datatype: 'json',
        contentType: 'application/json;charset=utf-8',
        type: 'POST',
        data: JSON.stringify({_resp: ajaxParameter}),
        success: function (data) {

            $.each(data, function (index, itemData) {
                $('#txtResponsavel').val(itemData.NM_Responsavel);
                $('#txtSigla').val(itemData.NM_Responsavel_Sigla);
                $('#cbxNivelResp option[value=' + itemData.IDNivel_Responsavel + ']').attr('selected', 'selected');
            })
        },
        error: function(error){
    }
    })
}

I do not know if this will solve your problem, but I do not have these kind of problems.

    
11.06.2015 / 17:40