user3557236
user3557236

Reputation: 299

Adding an array in jQuery and pass to controller

I created an array like so

obj={}
 obj['id'] = jQuery(this).attr("id");
 obj['slotNum'] = tNumber;

I need to add this to an array and send it to controller via ajax and access this array in controller in MVC. Can you please advice? I tries creating

var arr=[]; 
arr.push(obj)

When I put it in alert, I can't see any values. Am I putting it write and how to pass the above array to MVC controller and read it.

Upvotes: 1

Views: 915

Answers (1)

PeterKA
PeterKA

Reputation: 24648

obj is an object, not an array.

You can use JSON.stringify() to convert your array of objects into a string which can be sent in an ajax request:

var strArr = JSON.stringify( arr );

Then assign strArr a parameter name in your ajax request:

$.ajax({
    url: ....,
    .......
    data: {
        mydata: strArr,
        ....
    },
    ....
});

Upvotes: 1

Related Questions