Post Send data, but webapi capture null

-1

I'm getting this header from 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":""}]}

O submission to webAPI

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

At WEBAPI it's 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 have already added this line in webconfig

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

I only get NULL in parameter value .

What can it be? my IIS is connected as my local user (with password). Is there anything to see?

    
asked by anonymous 11.01.2016 / 20:13

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 this is to create a class that receives this data, taking advantage of the Model Binding feature that the Web API has native: p>

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

Controller , so it looks like this:

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

You are arriving null because your call does not match the signature of the method. Try changing the data to this:

data: { 'value' : comandaPojo }
    
19.02.2016 / 15:43