I have a javascript function that calls a method in the code behind for an asp.net c# application. I pass an array into JSON.stringify which returns the string equivalent to the jsonText value below. The values below represent only one of the records returned.
var jsonText = "[{\"eacItem\":{\"ID\":\"dc97d510-cd29-4a23-af7f-36c0acfa1914\",\"AP\":\"201407\",\"EAC\":\"0\"}}]";
The string is then passed into the function to call the method in the code behind.
$.ajax({
type: "POST",
traditional: true, //might be needed for serialization to work properly
url: pagePath + "/" + "TestMethod",
data: "{'eacArray':" + jsonText + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (data) {
alert("successfully posted data");
},
error: function (data) {
alert("failed posted data");
alert(postData);
}
});
This is the method in the code behind that is called.
[WebMethod]
public static void TestMethod(List<eacItem> eacArray)
{
//do something
}
This is the Class associated with the JSON data set as the parameter type for the method.
public class eacItem
{
public string ID { get; set; }
public string AP { get; set; }
public string EAC { get; set; }
}
The data passed into the method contains the record(s) but the values are all null as shown in the screenshot below. I am not sure why this is happening and would appreciate any help.


Additional info that was requested. Here is a screenshot of the value in eacArray.

This is the JSON string in debug

Result of JSON.stringify(eacArray) in data: "{'eacArray':" + JSON.stringify(eacArray) + "}",
"[{\"eacItem\":{\"AP\":\"201407\",\"EAC\":\"0\",\"ID\":\"dc97d510-cd29-4a23-af7f-36c0acfa1914\"}},{\"eacItem\":{\"AP\":\"201304\",\"EAC\":\"0\",\"ID\":\"69326e1b-8d69-431f-8a5b-29f8b279d74c\"}},{\"eacItem\":{\"AP\":\"201305\",\"EAC\":\"0\",\"ID\":\"69326e1b-8d69-431f-8a5b-29f8b279d74c\"}},{\"eacItem\":{\"AP\":\"201306\",\"EAC\":\"0\",\"ID\":\"69326e1b-8d69-431f-8a5b-29f8b279d74c\"}}]"
Here is the corrected code in case it can help anyone:
//Iterate through elements on page
$('ElementContainingValue').each(function () {
var ap = this.attributes.ap.value;
var id = this.attributes.id.value;
var eac = $(this).val();
var eacItem = { "AP": ap, "EAC": eac, "ID": id };
eacArray.push(eacItem);
});
var pagePath = window.location.pathname;
$.ajax({
type: "POST",
traditional: true, //might be needed for serialization to work properly
url: pagePath + "/" + "TestMethod",
data: "{'eacArray':" + JSON.stringify(eacArray) + "}",
// Contents of eacArray after stringify
// "[{\"AP\":\"201407\",\"EAC\":\"0\",\"ID\":\"dc97d510"},{\"AP\":\"201408\",\"EAC\":\"0\",\"ID\":\"dc97d510-cd29-4a23-af7f-36c0acfa1914\"}]
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (data) {
alert("successfully posted data");
},
error: function (data) {
alert("failed posted data");
alert(postData);
}
});
This is the method called in the code behind.
[WebMethod]
public static void TestMethod(List<eacItem> eacArray)
{
foreach (var eacItem in eacArray)
{
string ap = eacItem.AP;
string id = eacItem.ID;
string eac = eacItem.EAC;
}
}
This is the class associated with the array.
public class eacItem
{
public string ID { get; set; }
public string AP { get; set; }
public string EAC { get; set; }
}