I have a complex object in a SPA javascript that I need to send to the Api using Ajax. One of the properties is an array and I'm trying to map an array to the id property before sending the data to the Api. Here's the code:
This is the class (the id is a string in the database):
var ContactList = {
AcntNumber: 0,
Contacts: [
{
id: "",
name: ""
}
]
}
Then in another file, I'm getting the values from a dropdown and creating an array contacts. I'm having hard time to assign the array to the id of the ContactList.Contacts.
var users = [];
var ContactList = Object.assign({}, ContactList);
$('select#users').each(function () {
Array.prototype.push.apply(users, $(this).val());
});
ContactList.Contacts = Object.assign({}, users);
ContactList.AcntNumber = $("#accNum").val();
$.ajax({
type: "POST",
headers: { "Authorization": `Bearer ${token}` },
async: false,
url: URL,
data: JSON.stringify(ContactList),
contentType: "application/json",
dataType: "json",
success: function (data) {
console.log(data);
}
});
I keep getting the following error: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable.
Here's my Classes in the Api:
public class ContactList
{
public IEnumerable<Contact> Contacts { get; set; }
public Accounts Account { get; set; }
}
public class Contact
{
public string ID { get; set; }
public string Name { get; set; }
}
Any help would be greatly appreciated, thanks!