Skip to content Skip to sidebar Skip to footer

Passing Array From Javascript To Controller Mvc 4

I am using razor and I'm having a hard time passing an array to a controller. the array contains objects that I made and I am trying to do this: $.ajax({ type: 'POST', ur

Solution 1:

the Ajax parameter

traditional :true

will do the trick.

Solution 2:

Client side:

$.ajax({
     type: "POST",
     url: "HomePage/HandleOperations",
     data: {operations: operationCollection},
     success: function (data) { alert("SUCCESS"); }
});

and declare a class server side like this:

publicclassOperation
{
  publicint Index;
  publicstring Source;
  publicstring Target;
  publicint AddOrDel;
}

then you can have this action:

publicvoidHandleOperations(Operation[] operations){
}

Solution 3:

Usage of the traditional: true parameter for an ajax call:

To help radbyx, using the "traditional: true" property of an ajax call, like the following, will tell ajax to use the traditional form of serialization. More details: http://api.jquery.com/jQuery.param/ or What is "traditional style of param serialization' in JQuery.

$.ajax({
     type: "POST",
     url: "HomePage/HandleOperations",
     data: {operations: operationCollection},
     traditional: true,
     success: function (data) { alert("SUCCESS"); }
});

Solution 4:

Add "traditional:true" parameter in your ajax.

Example:

var parentValueToPush=newArray();
$('[name=SelectedUsers]:checked').each(function () {
            parentValueToPush.push($(this).val());
            temp.push($(this).val());
        });
        $.ajax({
            url: //URL,type: 'get',
            traditional: true,
            dataType: 'html',
            cache: false,
            data: { SelectedUsers: parentValueToPush},
            success: function (data) {
                   //Result
            }
        });

Post a Comment for "Passing Array From Javascript To Controller Mvc 4"