I have a function that takes the variables from a list, creates the objects, and inserts into a list of objects. But I need to pass this list to the mvc driver;
jquery function
$(function() {
jQuery.ajaxSettings.traditional = true;
$("#saveDiffer").on('click',
function(event) {
event.preventDefault();
event.stopPropagation();
var count = 0;
var differs = [];
$('.differ').each(function() {
var nome = $(this).children('input').val();
var tipo = $(this).find('li.active').find('span').text();
var differ = {
Nome: nome,
Tipo: tipo
};
differs.push(differ);
});
console.log(differs);
$.ajax({
type: 'POST',
url: '/Upload/SaveDiffer',
dataType: 'JSON',
data: JSON.stringify(differs),
contentType: 'application/JSON; charset=utf-8',
success: function(data){
console.log(data);
},
error: function(data) {
console.log(data)
}
});
});
});
Controller
[HttpPost]
public ActionResult SaveDiffer(List<AtividadeJsonModel> differs)
{
// Código aqui
return null;
}
Template
public class AtividadeJsonModel
{
public string Nome { get; set; }
public string Tipo { get; set; }
}
Edit
I tried to use [FromBody]
in the controller, as some answers suggest, but without success, it gets a null.