OK, in this case I will show you how I did this in several ways: First, I put the div inside the jquery with empty fields as follows:
$("#add").click(function() { $("#classes").append($( "<div>" +" <select name='Student.Classes[0].Class'>" +" <option value='1'>Class 1</option>" .... +" </select>" +" <select name='Student.Classes[0].Subject'>" +" <option value='1'>Subject 1</option>" .... +" </select>" +" <input name='Student.Classes[0].Score' value='0'/>" +"</div>" ) );});
This div will be added to the class list when it clicks something with id #add.
Next, I will catch the form before submitting and give each object (Student.Classes in this case) an index starting with 0. Example:
$("form").submit(function () { $("div", "#classes").each(function (i) { $(":input, :hidden", this).each(function () { $(this).attr("name", $(this).attr("name").replace(/\[0\]/, "[" + i + "]")); }); }); });
If you use a ModelBinder that supports child entities, this should end on the server with a list of classes in Student. Of course, you can use firebug to see exactly what is being sent to the server.
Hope this gives some direction.
source share