I have the following object graph and I'm using Jquery's $.Ajax() to send this identical View object in JSON (stringified) from the browser to a Page Method on ASP.Net. The JAvascript deserialization works for all of the strings and int's in the View class but My List<DataItem>
is empty.
What I tried: Using chrome dev tools, I took the stringified JSON, created a unit test and used both the DataContractJsonSerializer
and the JavaScriptSerializer
. The DataContractJsonSerializer
object deserialized my object graph correctly but the JavaScriptSerializer
dumped my List. How can I get the correct deserialization on my page method ?
public class View
{
public string Text { get; set; }
public string AnotherText { get; set; }
public Int SomeInt { get; set; }
public List<DataItem> { get; set; }
}
public class DataItem
{
public Person person {get;set}
}
public class Person
{
public int Age {get;set}
}
var dataa = {mqvm: mqvmJSON };
$.ajax({
type: POST,
dataType: json,
contentType: application/json; charset=utf-8,
data: JSON.stringify( dataa ),
url: GoHere.aspx/WebMethodName,
success: function(data) {
alert(data.d);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText + ' ' + errorThrown);
}
});
Instead of this (the view obj as a param).
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static string CreateResponseReview(View mqvm)
{
return Success;
}
how can I get this? (the string param)
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static string CreateResponseReview(string mqvm)
{
//do manual JSON deserialization here.
return Success;
}
My JSON looks like this.
{
Text: 6,
AnotherText:wow
SomeInt: 5,
DataItem:[
{
person:{
Age:23
}
},
{
person:{
Age:42
}
}
]
}