Übergeben einer Liste von Objekten an eine MVC-Controller-Methode mithilfe von jQuery Ajax
Ich versuche, ein Array von Objekten mit der Funktion ajax () von jQuery an eine MVC-Controller-Methode zu übergeben. Wenn ich in die PassThing () C # -Controller-Methode komme, ist das Argument "things" null. Ich habe dies mit einer Art Liste für das Argument versucht, aber das funktioniert auch nicht. Was mache ich falsch?
<script type="text/javascript">
$(document).ready(function () {
var things = [
{ id: 1, color: 'yellow' },
{ id: 2, color: 'blue' },
{ id: 3, color: 'red' }
];
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: '/Xhr/ThingController/PassThing',
data: JSON.stringify(things)
});
});
</script>
public class ThingController : Controller
{
public void PassThing(Thing[] things)
{
// do stuff with things here...
}
public class Thing
{
public int id { get; set; }
public string color { get; set; }
}
}