Click here to Skip to main content
15,994,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a simple JavaScript array that I am trying to pass to a controller

JavaScript
function SubmitData()
{
    var operationCollection = new Array();

    var test1 = { name: "Bill", age: "55", address: "testing" };
    operationCollection.push(test1);
    var test2 = { name: "Ben", age: "55", address: "testing" };
    operationCollection.push(test2);
    var test3 = { name: "Flo", age: "55", address: "testing" };
    operationCollection.push(test3);


    var dataToPost = JSON.stringify(operationCollection);


    $.ajax({
        type: "POST",
        url: "Home/AddPerson",
        datatype: JSON,
        data: { methodParam: dataToPost },
        traditional: true

    });
}


The C# controller has a class

C#
public class newEntry
{

    public string name { get; set; }
    public string age { get; set; }
    public string address { get; set; }

}


and a method

C#
public void AddPerson(List<newEntry> methodParam)
{

    foreach (newEntry item in methodParam)
    {
      string name =  item.age + item.address;

    }
}


What I have tried:

C#
When I run the code in debug, the value passed to the controller method is always NULL or 0. I can't seem to get the array to pass correctly. I have read on previous posts that traditional: true will fix this... it doesn't for me though. Has anyone any ideas?
Posted
Updated 14-Oct-16 4:17am

JavaScript
// use a normal array
var operationCollection = [];

var test1 = { name: "Bill", age: "55", address: "testing" };
operationCollection.push(test1);
var test2 = { name: "Ben", age: "55", address: "testing" };
operationCollection.push(test2);
var test3 = { name: "Flo", age: "55", address: "testing" };
operationCollection.push(test3);

// create a json object that has your collection as a property
// then stringify the whole object
dataToPost = JSON.stringify({ methodParam: operationCollection });

$.ajax({
    type: "POST",
    url: "/Home/AddPerson",
    contentType: "application/json; charset=utf-8", // specify the content type
    dataType: 'JSON', // make sure you use the correct case for dataType
    data: dataToPost,
    traditional: true
});
 
Share this answer
 
v2
Can you try ArrayList in place of List.
 
Share this answer
 
Comments
Member 11596389 14-Oct-16 7:13am    
Hi when i changed the list to an arraylist i just get zero
Hi,

In your code replace below line of code.

1)
var dataToPost = JSON.stringify({ methodParam: operationCollection });


2)
JavaScript
data: dataToPost,


And add below line that is :

JavaScript
contentType: "application/json; charset=utf-8",



It would work perfectly.
 
Share this answer
 
Comments
Richard Deeming 14-Oct-16 10:45am    
So basically, exactly what Solution 2 suggested, 2½ hours before you posted this?!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900