0

I am trying to pass two JSON Objects to my Page Method via AJAX

I created a class in Code Behind and created a JSON Object Array in javascript.

Following are my two classes and Page Method

public class userModel
{
    public string userid { get; set; }
    public string Name { get; set; }
}
public class companyModel 
{
    public string companyid { get; set; }
    public string Name { get; set; }
}
[WebMethod]
public static string TestJsonMethod(userModel[] users)
{

    return "";
}

And Following is my Javascript

function TestJSON() {
        //var JSONObject = { a: [], b };
        var obj = {users: []};
        var user;
        for (var i = 0; i < 3; i++) {
            user = {
                userid: i.toString(),
                Name: "User" + i.toString() 
            }
            obj.users.push(user);
        }

        var company = {companyid: "4", Name:"TEST COMPANY"};

        $.ajax({
            url: "bulkconsignments.aspx/TestJsonMethod",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(obj),
            async: false,
            cache: false,
            success: function (msg) {
                alert(msg.d);
            }
        });
    }

Up till this point my code is working fine.

What I want to do is to pass the company object to the Page Method as well like this

[WebMethod]
public static string TestJsonMethod(userModel[] users, companyModel company)
{

    return "";
}

2 Answers 2

1

You could merge the models into one model.

public class TestJsonModel
{
    public UserModel UserModel { get; set; }

    public CompanyModel CompanyModel { get; set; }
}

Then action looks like;

[WebMethod]
public static string TestJsonMethod(TestJsonModel model)

Also, Ajax request post data looks like;

data: JSON.stringify({UserModel:obj,CompanyModel:company})
Sign up to request clarification or add additional context in comments.

Comments

0

I was trying out different ways of doing it and this one worked.

function TestJSON() {
    //var JSONObject = { a: [], b };
    var obj = {users: [], company: {}};
    var user;
    for (var i = 0; i < 3; i++) {
        user = {
            userid: i.toString(),
            Name: "User" + i.toString() 
        }
        obj.users.push(user);
    }


    var company_ = {companyid: "4", Name:"TEST COMPANY"};
    obj.company = company_;
    $.ajax({
        url: "bulkconsignments.aspx/TestJsonMethod",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(obj),
        async: false,
        cache: false,
        success: function (msg) {
            alert(msg.d);
        }
    });
}

And the Code behind Page Method remains same.

 [WebMethod]
public static string TestJsonMethod(userModel[] users, companyModel company)
{

    return "";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.