Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON doesn't work with typed class in Webservice
    text
    copied!<p>I'm having a class like the following:</p> <pre><code>using System; using System.Collections.Generic; using System.Runtime.Serialization; [DataContract()] public class TestCol : List&lt;Test&gt; { } [DataContract()] public class MainTest { public TestCol Components { get; set; } } [DataContract()] public class Test { public Test() { } public String Name { get; set; } } </code></pre> <p>And a webservice with the following webmethod like this:</p> <pre><code>[WebMethod] public String Test(MainTest input) { String rtrn = String.Empty; foreach (Test test in input.Components) rtrn += test.Name; return rtrn; } </code></pre> <p>Which is called by AJAX with the following method:</p> <pre><code>var Test = {}; Test.Name = "Test"; var MainTest = {}; MainTest.Components = []; MainTest.Components.push(Test); $.ajax({ type: "POST", url: "WebService/WSTest.asmx/Test", contentType: "application/json; charset=utf-8", dataType: "json", data: JSON.stringify({ "input": MainTest }), success: function(data, textStatus) { console.log("success"); }, error: function(XMLHttpRequest, textStatus, errorThrown) { window.console &amp;&amp; console.log &amp;&amp; console.log(XMLHttpRequest.responseText + " || " + textStatus + " || " + errorThrown); } }); </code></pre> <p>When executing the AJAX call, it will return errors. I found out that the error is with the typed class <code>TestCol</code>, which has no properties. Now do I have found 2 solutions that require changes in the C# classes:</p> <ol> <li><p>Remove the <code>TestCol</code> class and change the <code>Components</code> property to <code>List&lt;Test&gt;</code> datatype:</p> <pre><code>[DataContract()] public class MainTest { public List&lt;Test&gt; Components { get; set; } } [DataContract()] public class Test { public Test() { } public String Name { get; set; } } </code></pre></li> <li><p>Or add an extra property to the <code>TestCol</code> class and change the webmethod:</p> <pre><code>[DataContract()] public class TestCol : List&lt;Test&gt; { public List&lt;Test&gt; Components { get; set; } } [DataContract()] public class MainTest { public TestCol Components { get; set; } } [DataContract()] public class Test { public Test() { } public String Name { get; set; } } </code></pre> <p>&amp;</p> <pre><code>[WebMethod] public String Test(MainTest input) { String rtrn = String.Empty; foreach (Test test in input.Components.Components) rtrn += test.Name; return rtrn; } </code></pre></li> </ol> <p>Both solutions require changes in the C# classes, which I prefer not to, as other code is depended on it. <strong>Does anyone know a solution for this problem?</strong></p> <p><em>Edit</em>: I've uploaded a test solution, containing above code: <a href="http://jeroenvanwarmerdam.nl/content/temp/JSONtoClassWebservice.zip" rel="nofollow">http://jeroenvanwarmerdam.nl/content/temp/JSONtoClassWebservice.zip</a></p>
 

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