Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7003719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:06:20+00:00 2026-05-27T21:06:20+00:00

I’m calling an asp.net webservice with JQuery and ajax, transfering data with json. I’m

  • 0

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 :

[DataContract]
[KnownType(typeof(TextareaObject))]
[KnownType(typeof(TextObject))]
public class FormElement
{
    public FormElement()
    {}
}

and :

  [DataContract(Name = "textObject")]
public class TextObject : FormElement
{
      [DataMember]
    public string question { get; set; }

    public TextObject(string question)
    {
            this.question = question;
}
}

and my webmethod :

 [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
[ServiceKnownType(typeof(TextObject))]
[ServiceKnownType(typeof(TextareaObject))]
public void SaveForm(List<FormElement> formobjects)
{
   ...

}

And here’s the way I’m creating javascript objects (I copy only relevant samples of my 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
            }
        });

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

 textObject.prototype = new textObject(typedquestion);
            objectarray.push(textObject.prototype);

and the json gives something like :

{"formobjects":{"textObject": {"question":"test"}}}

But server side same old same old, I only get FormElement type in my webmethod and I can’t cast.

maybe what I wanna do is not possible..
thank you anyway !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:06:21+00:00Added an answer on May 27, 2026 at 9:06 pm

    well I found my answer. Like rich.okelly said, there is no type information sent with weakly-typed language that is javascript, because javascript classes aren’t real classes like in .net. But Microsoft anticipated that, you need to use what they call “type hints”, it means you add “__type” property to your javascript objects, and in the first position like so :

        //objects to serialize
        function textObject(__type, question) {
            this.__type = __type;
            this.question = question;       
        }
    (...)
          var textobject1 = new textObject("textObject:#",typedquestion);
    

    or even add it to the class itself so you don’t add to set it each time:

        //objects to serialize
        function textObject(question) {
            this.__type = "textObject:#";
            this.question = question;       
        }
    (...)
          var textobject1 = new textObject(typedquestion);
    

    adding __type after question won’t work. Plus, even if there is no namespace you need to add “:#” after it. the schema follows : “datacontractname:#datacontractnamespace”.
    I found it programatically creating serializer on my TextObject so that I can find the needed format. Then my formelement received in my webmethod is type of TextObject ! job done !
    thank you all !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am reading a book about Javascript and jQuery and using one of the
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.