Also übergebe ich ein Array von Objekten wie
{ QuestionId: 24,
AnswerVal: -1,
PartnerId: "e0e8035d-046a-45c5-b7e5-7986865ce37c",
VersionId: 1 }
und mein Controller ist
[HttpPost]
public async Task<ActionResult> SubmitAnswers ( List<AnswerSubmission> A )
woher AnswerSubmission
ist definiert durch
public class AnswerSubmission
{
public Guid PartnerId { get; set; }
public int AnswerVal { get; set; }
public int QuestionId { get; set; }
public int VersionId { get; set; }
}
Es funktioniert nicht, da ich eine leere Antwort bekomme
$.ajax({
method: "POST",
url: "/Answers/SubmitAnswers",
dataType: "json",
data: JSON.stringify(Answers),
complete: function ( retobj ) {
console.log(retobj);
}
});
wenn ich meinen Controller mache
[HttpPost]
public async Task<ActionResult> SubmitAnswers ( List<AnswerSubmission> A )
{
return Json(A);
}
als Test.
Antworten:
1 für die Antwort № 1{ QuestionId: 24,
AnswerVal: -1,
PartnerId: "e0e8035d-046a-45c5-b7e5-7986865ce37c",
VersionId: 1 }
Ist kein "Array von Objekten"Es ist ein Objekt.
Damit in Ergänzung zu contentType: "application/json; charset=utf-8"
wie kommentiert von @Stephen Muecke
Jquery:
$.ajax({
method: "POST",
url: "/Answers/SubmitAnswers",
dataType: "json",
//add this
contentType: "application/json; charset=utf-8",
data: JSON.stringify(Answers),
complete: function (retobj) {
console.log(retobj);
}
}).done(function (d) {
console.log(d); //probably what you"re really looking for..
});
Regler:
[HttpPost]
public ActionResult SubmitAnswers(AnswerSubmission A)//an object, not an array of objects
{
return Json(A);
}