开发者

Creating a C# object in javascript

开发者 https://www.devze.com 2022-12-09 01:21 出处:网络
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, bu

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 C开发者_如何学JAVAlassTest
  {
    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)
    {
      ...
    }
  }
}


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.?


This should work for you

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号