I can not capture the post via webapi c #

0

Good morning. I am already trying a few days trying to capture a POST made in JavaScript by webapi c # and the most I can get is webapi telling me that the expected parameter is NULL.

var objFornecedor = {
                ID_FORNECEDOR: 38,
                NOME: "cleverton teste",
                CNPJ: "546546546546",
                ENDERECO: "rua c",
                BAIRRO: "novo teste",
                CIDADE: "serrinha",
                SITUACAO: "1",
                DATA_CADASTRO: new Date(2015,02,02)
    }


    $.ajax({
                  contentType: 'application/json; charset=utf-8',
    data: objFornecedor,
          url: 'http://localhost:3190/servicowebapi/fornecedor/incluir',
      success: function(retorno)
        {
              alert('funcionou');..........

and here is the code you receive

  [HttpPost]
  [ActionName("incluir")]
  public void Post(FORNECEDOR objFornecedor)
  {
      ctx.FORNECEDOR.Add(objFornecedor);
      ctx.SaveChanges();
  }

I get this error. An exception of type 'System.ArgumentNullException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Value can not be null. ctx.FORNECEDOR.Add (objFolder); object is NULL

and here is my SUPPLIER class

public partial class FORNECEDOR
{
    public int ID_FORNECEDOR { get; set; }
    public string NOME { get; set; }
    public string CNPJ { get; set; }
    public string ENDERECO { get; set; }
    public string BAIRRO { get; set; }
    public string CIDADE { get; set; }
    public string SITUACAO { get; set; }
    public Nullable<System.DateTime> DATA_CADASTRO { get; set; }
}
    
asked by anonymous 03.11.2015 / 12:35

2 answers

2

Use JSON.stringify :

$.ajax({
    type:'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(objFornecedor),
...
    
03.11.2015 / 13:57
0

Try this:

var objFornecedor = {
        ID_FORNECEDOR: 38,
        NOME: "cleverton teste",
        CNPJ: "546546546546",
        ENDERECO: "rua c",
        BAIRRO: "novo teste",
        CIDADE: "serrinha",
        SITUACAO: "1",
        DATA_CADASTRO: new Date(2015, 02, 02)
    };

    $.ajax({
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(objFornecedor),
        type: 'POST',
        url: 'http://localhost:3190/servicowebapi/fornecedor/incluir',
        success: function (retorno) {
            alert('funcionou');
        }
    });
    
03.11.2015 / 15:03