List Null Post Ajax Asp.NET MVC 5

Asked

Viewed 45 times

0

I’m trying to post a LIST via ajax, only the controller is always getting null.

Requisition code:

var Componentes = [{ Nome: "Teste1" }, {Nome: "Teste2"}];
$.ajax({
         type: "post",  
         url: '@Url.Action("Cadastrar","DocumentoDinamico")',
         data: JSON.stringify(Componentes),  
         datatype: "json",  
         cache: false,  
         success: function (data) {
              console.log('Sucesso.');
         },  
         error: function(data) {  
             console.log('Erro. ' + data);
         } 
 });

Model code:

public class Componente
{
    public string Nome { get; set; }
}

Controller code:

public ActionResult Cadastrar(List<Componente> Componentes)
{
    return View("Index");
}

1 answer

1

I managed to resolve added this code in the request:

 contentType: 'application/json; charset=utf-8'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.