I am trying to post a complex JSON object to my C#/MVC controller. The object looks like this:
public class RootObject
{
public Guid Id { get; set; }
public List<ChildObject> Children { get; set; }
}
public class ChildObject
{
public HttpPostedFileBase UploadedDocument { get; set; }
// Other properties here, but left out of this example
}
And my controller action looks like this:
[HttpPost]
public JsonResult UploadFiles(RootObject dto)
{
// Do stuff with root object and each file in each child object.
}
My JavaScript/jQuery code looks like this:
var formData = new FormData;
formData.append('Id', 'some_id');
var index = 0;
$('.member').each(function () {
formData.append('Children[' + index + '][UploadedDocument]', document.getElementById(id).files[0]);
index++;
});
$.ajax({
type: "POST",
url: endpointUrl,
processData: false,
contentType: false,
data: formData,
success: function (responseData) {
},
error: function (res) {
}
});
This just won't work. C#/MVC does not receive the UploadedDocument. If (for testing purposes only) I move the UploadedDocument property to the RootObject, turn it into a collection of HttpPostedFileBase and post the files directly to the root, then all works, but that is not a solution since the ChildObject has other properties I need to post as well.
Any ideas? My conclusion right now is that it is not possible to post files to children of c# objects via MVC.