Skip to content

How to add array to form when using jquery submit?

I have a form on a page and the data inputed gets posted to the server. I am right now tying to add an array to the post like so:

$("#createTankForm").submit(function() {
    if ($(this).valid()) {
        var data = $(this).serializeArray();
        var celVerLst = [];
        var formsLst = $(".TankCalVertList").find("#createTankForm .adminRow");
        $(formsLst).each(function (i, v) {
            var celVert = {
                Number: $(this).find("#Number").val(), 
                Border: $(this).find("#Border").val(),
                Volume: $(this).find("#Volume").val(),
                Constant: $(this).find("#Constant").val(),
            }
            celVerLst.push(celVert);
        });
        data.push({
            name: "TankCalVerts",
            value: celVerLst
        });
        data = jQuery.param(data);
        // at this point TankCalVerts is "object[]"
        $.automation.worker.postUserData(this.action, data, function(data) {
            $(".AdmClicked").click();
        });
    } else {
        $(this).addClass("invalidForm");
    }
    return false;
});

As written in the comment above I get

TankCalVerts=%5Bobject+Object%5D%2C%5Bobject+Object%5D

in the post

And in the action method:

enter image description here

How do I do this?

EDIT:

postUserData: function(url, data, callback) {
    //$.LoadingOverlay("show");
    $.ajax({
        url: url,
        type: 'POST',
        data: data,
        success: function(data) {
            if (callback) {
                callback(data);
                //$.LoadingOverlay("hide");
            }
        },
    });
}

Answer

After some thinkering and some help from a brilliant friend we came up with this:

                        var objFormData = {};
                        for (var intIndex = 0; intIndex < data.length; intIndex++) {
                            objFormData[data[intIndex].name] = data[intIndex].value;
                        }

We took the serialized data and turned it in to an object which we then posted as a JSON.

I had done a solution where I built my own object but in thise case it would be up to 30 paramaters and i was trying to avoid it and with the simple loop above it solved that issue.

Full answer:

                        var objFormData = {};
                        for (var intIndex = 0; intIndex < data.length; intIndex++) {
                            objFormData[data[intIndex].name] = data[intIndex].value;
                        }

                        $.automation.worker
                            .postJson(this.action,
                                JSON.stringify(objFormData),
                                function(data) {});

post json function:

    postJson: function(url, data, callback) {          
        $.LoadingOverlay("show");

        $.ajax({
            url: url,
            type: "POST",
            data: data,
            contentType: "application/json; charset=utf-8",
            success: function(data) {
                if (callback)
                    callback(data);

                $.LoadingOverlay("hide");
            }
        });