Массив Javascript для контроллера MVC
У меня есть простой массив 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 }); }
Контроллер C# имеет класс
public class newEntry { public string name { get; set; } public string age { get; set; } public string address { get; set; } }
и метод
public void AddPerson(List<newEntry> methodParam) { foreach (newEntry item in methodParam) { string name = item.age + item.address; } }
Что я уже пробовал:
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?