I'm facing an issue with my JavaScript array when trying to send it to a controller.
function SubmitData()
{
var dataCollection = new Array();
var test1 = { name: "Alice", age: "30", address: "123 Main St" };
dataCollection.push(test1);
var test2 = { name: "Bob", age: "40", address: "456 Elm St" };
dataCollection.push(test2);
var test3 = { name: "Charlie", age: "50", address: "789 Oak St" };
dataCollection.push(test3);
var jsonDataToPost = JSON.stringify(dataCollection);
$.ajax({
type: "POST",
url: "Home/SaveEntry",
datatype: JSON,
data: { inputData: jsonDataToPost },
traditional: true
});
}
The C# controller includes the following class:
public class EntryDetails
{
public string name { get; set; }
public string age { get; set; }
public string address { get; set; }
}
and this method:
public void SaveEntry(List<EntryDetails> inputData)
{
foreach (EntryDetails item in inputData)
{
string details = item.name + item.age + item.address;
}
}
Even though I have set traditional: true
, when debugging, the value passed to the controller method is always NULL or 0. The array does not seem to be passing correctly. Any suggestions on how to resolve this issue?