0

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
  • As mentioned in the answer, you need [FormBody]. This article might help give you some background. Commented Mar 14, 2019 at 22:45

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Hello @Alexander! It did not work ... Still the same problem :(
It worked!!! I changed the parameter from: "data: JSON.stringify (requestData)" to: "data: JSON.stringify (operadoraChamadaViewModel)". Thank you very much!!!!! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.