Problems with Post Ajax in Asp.net core MVC

2020-05-06 17:12发布

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; }
}

1条回答
可以哭但决不认输i
2楼-- · 2020-05-06 17:30

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)
查看更多
登录 后发表回答