Collection always null when using [FromForm] attribute Collection always null when using [FromForm] attribute json json

Collection always null when using [FromForm] attribute


For test I used the following arrayOfContent:

var arrayOfContent = [];arrayOfContent.push({ name: 'test', link: 'test.com' });arrayOfContent.push({ name: 'test2', link: 'test2.com' });

And I used a for loop to append the array to the form data:

for (var i = 0; i < arrayOfContent.length; i++) {    formData.append("Contents[" + i + "].Name", arrayOfContent[i].name);    formData.append("Contents[" + i + "].Link", arrayOfContent[i].link);}

And I in Visual Studio I can see that it can bind it:

enter image description here


You need to convert your array to JSON string in ajax submit code like this

const formData = new FormData();formData.append("File", myFile, "image.jpg")formData.append("Contents",  JSON.stringify(arrayOfContent))

Then deserialize in your controller