Note that there are some explanatory texts on larger screens.

plurals
  1. POError sending JSON object to asp.net WebMethod, using jQuery
    primarykey
    data
    text
    <p>I get an error of "Cannot convert object of type System.String' to type 'System.Collections.Generic.List'1[...+ContactHolder]'"</p> <p>This is my C# code:</p> <pre><code>public class ContactHolder{ public String Name { get; set; } public String Address { get; set; } } [WebMethod] public static string AddContact(Object items){ List&lt;ContactHolder&gt; contacts = new JavaScriptSerializer().ConvertToType&lt;List&lt;ContactHolder&gt;&gt;(items); return String.Format("You added {0} contacts!", contacts.ToString()); } </code></pre> <p>This is my Javascript code (note I have included jquery-1.4.1.js and json2.js in my project)</p> <pre><code>function Test() { var arr = new Array(); var contact = new Object(); contact.Name = "Ben"; contact.Address = "Ohio"; var contact2 = new Object(); contact.Name = "Jon"; contact.Address = "Illinois"; arr[1] = contact; arr[2] = contact2; var DTO = JSON.stringify(arr); PageMethods.AddContact(DTO, OnMyMethodComplete);} function OnMyMethodComplete(result, userContext, methodName) {alert(result);} </code></pre> <p>When I pass DTO into myPageMethod it looks like this: "[{"Name":"Ben","Address":"Ohio"},{"Name":"Jon","Address":"Illinois"}]"</p> <p>When it appears in my WebMethod as items it looks like this: "[{\"Name\":\"Ben\",\"Address\":\"Ohio\"},{\"Name\":\"Jon\",\"Address\":\"Illinois\"}]"</p> <p>The code errors on the call to JavaScriptSerializer().ConvertToType, the message is {"Cannot convert object of type System.String' to type 'System.Collections.Generic.List'1[...+ContactHolder]'"}</p> <p>What do I need to do to make this work? </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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