I have a class that I am serializing but I am changing the property names for the output string using [JsonProperty("Name")]
attribute annotation. Like below:
[JsonProperty("Name")]
public string PersonName{ get; set; }
Now when I want to get the data back the values cannot map to properties so they are set to null.
This is how I get data:
[WebMethod]
public static void GetData(List<Person> persons)
{
//each persons Name property comes as null
}
This is how I send data from client:
$.ajax({
type: "POST",
url: "TestPage.aspx/GetData",
data: "{'persons':" + '[{ "Name": "Me"}]' + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("Data Submitted");
}
});
Now I cannot stop .NET from serializing the JSON string that I am passing from client so I have to let my Page Method accept a parameter type of List<Person>
or else I will get error which is also preventing me from using JsonConvert.DeserializeObject<List<Person>>(person);
which will solve the problem of mapping.
So, someone please take time to read the post and give me some ideas.
Your web method is accepting a list of Persons, but that is not what you are passing from the client. You are passing an object that contains a list of persons. If you want it to work, you should just pass the list itself and not wrap it in an object.
Another alternative, if you can't change the client, is to change the server side to expect the wrapper object.
Create a class to hold the list...
...and change your web method to accept that class.