Post Sends data but webapi captures null

Asked

Viewed 778 times

-1

I’m taking this header from the Fiddler.

POST http://localhost:8887/api/values HTTP/1.1 
Host: localhost:8887
Proxy-Connection: keep-alive
Content-Length: 352
Accept: */*
Origin: http://localhost:8383
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:8383/comandaApp/principal.html
Accept-Encoding: gzip, deflate
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
{"IDEmpresa":"1","IDTabelaPreco":"1","numeroVendedor":"1","IDCupomFiscal":"","IDGrupo":"1","IDUsuario":"1","IDVendedor":"1","NMMesa":"3","IDMesa":"3","STCartao":"N","STViagem":"N","STDelivery":"N","modalidadeDelivery":"","observacaoDelivery":"","localEntrega":"","comandaItemPojo":[{"IDProduto":"162","NRReferencia":"","QTDProduto":1,"observacao":""}]}

Sending to the webAPI

$.ajax({
        type: 'POST',
        crossDomain: true,
        url: "http://localhost:8887/api/values",
        data: JSON.stringify(comandaPojo),
        success: function(retorno)
        {
        //códigos....
    }

In the WEBAPI is like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;

namespace webapi_MVC5.Controllers
{
    public class ValuesController : ApiController
    {
        public void Post([FromBody]string value)
        {
            string a = value; //VALUE RETORNA NULL
            string b = "x";
        }
    }
}

I’ve already added this line to webconfig

<add name="Access-Control-Allow-Origin" value="*" />

I only get NULL in the parameter value.

What can it be? my IIS is logged in as my local user (with password). It has something to do with?

  • you really want to catch the JSON string?

  • what has in comandaPojo? what is the content?

2 answers

1

This is the wrong way to pass parameters to your Controller.

If the idea is to pass a complex type, the best way to do it is to create a class that receives this data, taking advantage of the resource of Model Binding that the Web API has native:

public class ValoresViewModel
{
    public int IDEmpresa { get; set; }
    public int IDTabelaPreco { get; set; }
    public int numeroVendedor { get; set; }
    public int IDCupomFiscal { get; set; }
    public int IDGrupo { get; set; }
    public int IDUsuario { get; set; }
    public int IDVendedor { get; set; }
    public int NMMesa { get; set; }
    public int IDMesa { get; set; }
    public String STCartao { get; set; }
    public String STViagem { get; set; }
    public String STDelivery { get; set; }
    public String modalidadeDelivery { get; set; }
    public String observacaoDelivery { get; set; }
    public string localEntrega { get; set; }
    public ICollection<ComandaItemViewModel> comandaItemPojo { get; set; }
}

And ComandaItemViewModel will be:

public class ComandaItemViewModel
{
    public int IDProduto { get; set; }
    public String NRReferencia { get; set; }
    public int QTDProduto { get; set; }
    public String observacao { get; set; }
}

The Controller, so, here’s the deal:

public class ValuesController : ApiController
{
    [HttpPost]
    public void Post(ValoresViewModel viewModel)
    {
        // Coloque sua lógica de negócios aqui, usando os dados de viewModel.
    }
}

0

You are arriving null because your call does not match the method signature. Try changing the data for that reason:

data: { 'value' : comandaPojo }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.