Using NickW's suggestion, I was able to get this working using things = JSON.stringify({ 'things': things }); Here is the complete code.
$(document).ready(function () { var things = [ { id: 1, color: 'yellow' }, { id: 2, color: 'blue' }, { id: 3, color: 'red' } ]; things = JSON.stringify({ 'things': things }); $.ajax({ contentType: 'application/json; charset=utf-8', dataType: 'json', type: 'POST', url: '/Home/PassThings', data: things, success: function () { $('#result').html('"PassThings()" successfully called.'); }, failure: function (response) { $('#result').html(response); } }); }); public void PassThings(List<Thing> things) { var t = things; } public class Thing { public int Id { get; set; } public string Color { get; set; } }
From this I learned two things: 1) The parameters contentType and dataType are absolutely necessary in the ajax () function. This will not work if they are missing. I found this after much trial and error. 2) To pass an array of objects to the method of the MVC controller, simply use the JSON.stringify ({'things': things}) format.
I hope this helps someone else!
Halcyon Nov 06 '12 at 16:38 2012-11-06 16:38
source share