Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy is the json returning null values
    primarykey
    data
    text
    <p>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. </p> <pre><code>var jsonText = "[{\"eacItem\":{\"ID\":\"dc97d510-cd29-4a23-af7f-36c0acfa1914\",\"AP\":\"201407\",\"EAC\":\"0\"}}]"; </code></pre> <p>The string is then passed into the function to call the method in the code behind.</p> <pre><code>$.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); } }); </code></pre> <p>This is the method in the code behind that is called.</p> <pre><code>[WebMethod] public static void TestMethod(List&lt;eacItem&gt; eacArray) { //do something } </code></pre> <p>This is the Class associated with the JSON data set as the parameter type for the method.</p> <pre><code>public class eacItem { public string ID { get; set; } public string AP { get; set; } public string EAC { get; set; } } </code></pre> <p>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.</p> <p><img src="https://i.stack.imgur.com/XksZw.jpg" alt="null array"></p> <p><img src="https://i.stack.imgur.com/BSbDU.jpg" alt="in code behind"></p> <p>Additional info that was requested. Here is a screenshot of the value in eacArray. <img src="https://i.stack.imgur.com/V02ae.jpg" alt="eacArray"></p> <p>This is the JSON string in debug <img src="https://i.stack.imgur.com/mhC7Y.jpg" alt="JSON"></p> <p>Result of JSON.stringify(eacArray) in data: "{'eacArray':" + JSON.stringify(eacArray) + "}",</p> <p>"[{\"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\"}}]"</p> <p><strong>Here is the corrected code in case it can help anyone:</strong></p> <pre><code> //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); } }); </code></pre> <p>This is the method called in the code behind.</p> <pre><code> [WebMethod] public static void TestMethod(List&lt;eacItem&gt; eacArray) { foreach (var eacItem in eacArray) { string ap = eacItem.AP; string id = eacItem.ID; string eac = eacItem.EAC; } } </code></pre> <p>This is the class associated with the array.</p> <pre><code> public class eacItem { public string ID { get; set; } public string AP { get; set; } public string EAC { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload