C# WebMethod - Send and receive same custom object as parameter

1.9k views Asked by At

mycode:

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

}

page (WebMethods):

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

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

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

I send the object to the clientside normally, but can not receive it back to server. Why can not receive it back if the object is the same and their properties as well. where is the problem?

3

There are 3 answers

0
Iago Correia Guimarães On BEST ANSWER

I solved the problem by creating a constructor with no parameters , setting all properties as string and adding the set method on all properties on my custom object (Person).

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

    }
0
bateloche On

Looking at the very link provided by you is possible to notice the problem.

Your code: data: JSON.stringify({"oPerson": Person}),

Right code: data: "{oPerson:" + JSON.stringify(Person) + "}",

It seems to me that you're sending a bad formatted Json to the server

Also, try adding dataType: 'json' to your call.

5
Siamak Ferdos On

You are returning an object from webService, but your content type in ajax is json! You should create data in json format in both of your methods and return a string not object:

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

For second method just remove content-type from ajax or replace it by :

application/x-www-form-urlencoded; charset=UTF-8