2
I have a function that takes the variables from a list, creates the objects, and inserts them into a list of objects. But I need to pass this list to the mvc controller;
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;
}
Model
public class AtividadeJsonModel
{
public string Nome { get; set; }
public string Tipo { get; set; }
}
Edit
I tried to use the [FromBody]
in the controller, as some responses suggest, but to no avail, it receives a null.
Still differs in controller is set to Count 0
– Weslley Rocha