Problems with Post Ajax in Asp.net core MVC

萝らか妹 提交于 2019-12-20 06:19:10

问题


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?

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:


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)


来源:https://stackoverflow.com/questions/55172797/problems-with-post-ajax-in-asp-net-core-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!