我可以将两个用户定义的对象(如 JSON)传递给 ASP 中的 Web 方法吗?

Can I pass TWO user defined objects (as JSON) to web method in ASP?

对象类型:

Public Class X

Public p1 As Boolean

Public p2 As String

Public p3 As String

结束Class

Public Class Y

Public p1 As Boolean

Public p2 As String

Public p3 As String

结束Class

网络方法:

<WebMethod> _
Public Shared Function saveObject(obj As X) As Integer 

    ' stuff

End Function

Ajax:

var obj = { p1: value, p2: value, p3: value }; //object 1 which I successfully sent alone

var obj2 = { p1: value, p2: value, p3: value }; //object 2 that I want to send it together with object 1

. . .

. . .

data: JSON.stringify({ 'obj': obj }), //the data in AJAX call, which works perfectly with one object

现在我想发送 obj2(Y 型)和 obj1(X 型)...

有没有办法做到这一点或者不可能?

我尝试了所有我知道的方法,但我总是在 AJAX 呼叫中遇到错误或根本没有响应。

编辑:

好的,这是我的 javascript,我在其中创建我的对象并将其与 ajax 一起发送到我的控制器:

       $(document).ready(function () {


    //My test values
    var value = "test";
    var valueBool = true

    //The array
    var arrayObj = new Array();

    //My two obj
    var obj = { p1: valueBool, p2: value, p3: value };
    var obj2 = { p1: valueBool, p2: value, p3: value };

    //Add in the array
    arrayObj.push(obj);
    arrayObj.push(obj2);


        //Display before send it 
        console.log(JSON.stringify(arrayObj));
        $.ajax({
            type: 'POST',
            url: '/SendObj',
            data: { 'objArray': JSON.stringify(arrayObj) },
            dataType: "text",
            success: function (result) {

                if (result === "OK") {


                 console.log("Success");
                }
               else
                {
                console.log("Not good");

                }

            },
            error: function (req, status, error) {

            }
        });     

});

这是我的控制器,在 c# mvc 项目中测试过:

    public string SendObj(string objArray)
    {

        if (!String.IsNullOrWhiteSpace(objArray))
        {
         List<Y> objs =   JsonConvert.DeserializeObject<List<Y>>(objArray);
           //Here you have your list of objects, 1 or more , as you wish, do what you want to do with them.

            return "OK";
        }

        return "KO";


    }

然后是我用于此测试的 class Y:

       public class Y
    {


        public Boolean p1;

        public String p2;

        public String p3;

    }

您使用 json 发送数据,当您从客户端到服务器工作时,没有其他方式。然后你在你的控制器中使用你的对象,并将它们从字符串转换为你的特定 class。

您应该创建一个容器 class,其中包含您需要作为属性发送的 classes,并在 Ajax 请求中使用该 class。

Public Class X
    Public P1 As Boolean
    Public P2 As String
    Public P3 As String
End Class

Public Class Y
    Public P1 As Boolean
    Public P2 As String
    Public P3 As String
End Class

创建一个容器 class,其属性为 classes XY

Public Class Z
    Public XObj As X
    Public YObj As Y
End Class

然后用classZ的对象作为参数修改你的web方法。

<WebMethod> _
Public Shared Function saveObject(obj As Z) As Integer 
       // stuff 
End Function

现在在您的 Ajax 请求中将代码修改为

var xObj = { p1: value, p2: value, p3: value }; // object of class X
var yObj = { p1: value, p2: value, p3: value }; // object of class Y

var zObj = {xObj: xObj, yObj:yObj}; // object of class Z that contains objects of classes X and Y

data = JSON.stringify({'obj':zObj}); // send this object of class Z as data.