Whenever I attempt to send a JSON object to my controller, the parameter isn't being received.
This is the JavaScript code snippet:
var input = document.getElementById("input");
input.addEventListener('change', function () {
const reader = new FileReader();
reader.readAsText(input.files[0]);
reader.onload = () => {
const text = reader.result;
var result = text.replace(/([0-9":])/g, '');
result = result.split(/\r?\n/);
console.log(result);
var arr = new Array();
result.map(function (element) {
var cat = {
'Name': element.replace(/[\u200B-\u200D\uFEFF]/g, '')
};
arr.push(cat);
})
jsonObject = JSON.stringify(arr);
console.log(jsonObject);
$.ajax({
url: 'Categoria/BulkPost',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'JSON',
data: jsonObject,
traditional: true,
success: function () {
alert('Success!');
}
});
}
});
This is the relevant part of my controller:
[HttpPost]
public JsonResult BulkPost(List<Categoria> categorias)
{
//Perform some magic here
}
The model used in this process:
[DataContract]
public class Categoria
{
[Key]
public int IdClas { get; set; }
[Required]
[DataMember(Name = "Name")]
[DisplayName("Category Name")]
public string Name { get; set; }
}
And here is the generated JSON structure:
[
{
"Name":"Functional Aids"
},
{
"Name":"MEDICAL EQUIPMENT AND HOSPITAL SUPPLIES"
},
{
"Name":"Inventory"
},
...
]
I've attempted various solutions found in similar queries but none have worked for me :(. Hopefully, someone can assist me with this issue.