Master JR
Master JR

Reputation: 231

Problems with Post Ajax in Asp.net core MVC

I can not send a Model that I am manually creating to my controller. When I send the request, it's coming up with empty properties. There is something wrong that is hindering the conversion. Does anyone know how to help me?

enter image description here

var operadoraChamadas = {
    Id: 0,
    Descricao: 'rssrrssr',
    PadraoSistema: true
};

var requestData = { operadoraChamadasViewModel: operadoraChamadas}

$.ajax({
    url: "/pessoa-gerenciar/changeFormaContato",
    type: "POST",
    data: JSON.stringify(requestData),
    contentType: "application/json",
    dataType: "json",
    success: function (result) {
        alert('ok');
    },
    error: function () {
        alert("Oops! Algo deu errado.");
        console.log(requestData);
    }
});


[HttpPost]
[Route("pessoa-gerenciar/changeFormaContato")]
public IActionResult changeFormaContato(OperadoraChamadaViewModel operadoraChamadaViewModel)
{
    //ViewBag.indice_new = indice;
    //return PartialView("~/Views/Pessoa/PessoaContato/_PessoaContatoAdd.cshtml", _pessoaContatoAppService.CreateNew(pessoaNatureza, formaContatoId));
    return null;
}

ViewModel:

public class OperadoraChamadaViewModel
{
    [Key]
    [DisplayName("ID")]
    public int Id { get; set; }

    [Required(ErrorMessage = "A Descrição é obrigatória")]
    [MaxLength(50)]
    [DisplayName("Descricao")]
    public string Descricao { get; set; }

    [DisplayName("Padrão do Sistema")]
    public bool PadraoSistema { get; set; }
}

Upvotes: 0

Views: 180

Answers (1)

Alexander
Alexander

Reputation: 9632

ASP.NET Core requires to add [FromBody] attribute to parameter to parse application/json content

[HttpPost]
[Route("pessoa-gerenciar/changeFormaContato")]
public IActionResult changeFormaContato([FromBody] OperadoraChamadaViewModel operadoraChamadaViewModel)

Upvotes: 2

Related Questions