질문:
간단한 자바스크립트 배열이 있는데 컨트롤러에 전달하려고 합니다.
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;
}
}
디버그에서 코드를 실행할 때 디렉터에 전달되는 메서드의 값은 항상 NULL 또는 0입니다.나는 수조를 정확하게 통과시킬 수 없을 것 같다.나는 이전의 게시물에서
traditional: true
이 이 문제를 해결할 것이라고 읽었다...하지만 나한텐 중요하지 않아.어떤 생각 있으신 분?
답안
메소드의 서명에 추가하려고 시도합니다.
[HttpPost]
public void AddPerson(List<newEntry> methodParam)
가문에서 말한 바와 같이 사용
data: dataToPost