tags:

views:

106

answers:

2

I'm trying to submit data to a web service in my project. The data's in the form of several fields, so I'd rather create a single object and submit that. I know I can submit it as a JSON object, but I thought I'd seen code somewhere that would create a C# object on the JS side so that I could submit it to the web service.

Am I crazy, or what does this JS code look like?

Just to get everyone on the same page example-wise, let's say the C# class would look like this:

namespace NSTest
{
  [Serializable]
  public class ClassTest
  {
    public string ClassName;
    public int ClassValue;
  }
}

And the web service would look like this:

namespace NSTest
{
  public class WebServiceTest
  {
    [WebMethod]
    public void WSFunc(ClassTest test)
    {
      ...
    }
  }
}
+1  A: 

Javascript variable would look like this:

var preparedParameters = {
    ClassName: "Some name",
    ClassValue: 10
};

But it depends how do you intend to call your web service, because you may have to wrap this variable inside some other object/array etc.?

Just for the better ease of client compatibility, have you maybe considered WCF web services because they are more configurable and provide bare format that doesn't need any wrapping etc.?

Robert Koritnik
I haven't considered WCF yet since I haven't gotten any practice with it, but I'll look into it if I get some time. Thanks!
Adam V
+1  A: 

This should work for you

var test = {  
    ClassName:"classname",
    ClassValue:1
 };
Bob