Note that there are some explanatory texts on larger screens.

plurals
  1. POwcf deserializing : can we infer javascript type?
    text
    copied!<p>I'm calling an asp.net webservice with JQuery and ajax, transfering data with json. I'm creating javascript objects that will be json stringified. I need my webmethod to retrieve these particular object types but my parameter type is a base class and those objects inherit from my base class like this :</p> <pre><code>[DataContract] [KnownType(typeof(TextareaObject))] [KnownType(typeof(TextObject))] public class FormElement { public FormElement() {} } </code></pre> <p>and : </p> <pre><code> [DataContract(Name = "textObject")] public class TextObject : FormElement { [DataMember] public string question { get; set; } public TextObject(string question) { this.question = question; } } </code></pre> <p>and my webmethod : </p> <pre><code> [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] [OperationContract] [ServiceKnownType(typeof(TextObject))] [ServiceKnownType(typeof(TextareaObject))] public void SaveForm(List&lt;FormElement&gt; formobjects) { ... } </code></pre> <p>And here's the way I'm creating javascript objects (I copy only relevant samples of my code) : </p> <pre><code> //objects to serialize function textObject(question) { this.question = question; } //objects to serialize function textareaObject(question, rownumber) { this.question = question; this.rownumber = rownumber; } var objectarray = new Array(); if (type == 'text') { textobject1 = new textObject(typedquestion); objectarray.push(textobject1); } else if (type == 'textarea') { var rownumber = $(elm).children('textarea').attr('rows'); textareaobject1 = new textareaObject(typedquestion, rownumber); objectarray.push(textareaobject1); } var formobjects = JSON.stringify(objectarray); $.ajax({ type: "POST", //Page Name (in which the method should be called) and method name url: urlhtml, data: '{"formobjects":' + formobjects + '}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { //dosmth } }); </code></pre> <p>And I want asp.net server to be able to deserialize the correct type in my array. But once in my webmethod, "formobjects" are all of type FormElement, I can't get their real type even with serviceknowntype attributes. Is is because javascript isn't strongly typed that I can't retrieve the concrete types ? because the stringified json won't give the concrete type? I tried with </p> <pre><code> textObject.prototype = new textObject(typedquestion); objectarray.push(textObject.prototype); </code></pre> <p>and the json gives something like :</p> <pre><code>{"formobjects":{"textObject": {"question":"test"}}} </code></pre> <p>But server side same old same old, I only get FormElement type in my webmethod and I can't cast.</p> <p>maybe what I wanna do is not possible.. thank you anyway !!</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