How to send 1 object and 1 vector to a C# API with $http.post?

Asked

Viewed 50 times

1

In my application I created a C#API, which is consumed by my web application in Angular. I need to pass 1 object and 1 vector as parameters to the API, but the content arrives empty in the API, how to solve?

Follow below my codes:

Javascript part:

    $scope.Exportar = function () {

   var listProdutos = {
       ListaCompraVenda : {listacompravenda: $scope.modelcompraevenda.listProdutos},
       ListaMeses : {meses:[$scope.dataBase.data1,
       $scope.dataBase.data2,
       $scope.dataBase.data3,
       $scope.dataBase.data4,
       $scope.dataBase.data5,
       $scope.dataBase.data6,
       $scope.dataBase.data7,
       $scope.dataBase.data8,
       $scope.dataBase.data9,
       $scope.dataBase.data10,
       $scope.dataBase.data11,
       $scope.dataBase.data12]}
    };


    $http.post($rootScope.raiz_ws + "/local/funcao", listProdutos )
     .then(function (response) {
        }); 
    };

Controller in C#

 [HttpPost]
    [Route("funcao")]
    public HttpResponseMessage Gerar(Classe listProdutos)
    {..}

Class in C#:

public class Classe
{
    public List<CompraeVenda> ListaCompraVenda { get; set; }
    public List<string> ListaMeses { get; set; }

}

2 answers

1


I managed to solve by only changing my var listProducts

var listProdutos = {
        ListaCompraVenda: $scope.modelcompraevenda.listProdutos,
        ListaMeses: [$scope.dataBase.data1,
        $scope.dataBase.data2,
        $scope.dataBase.data3,
        $scope.dataBase.data4,
        $scope.dataBase.data5,
        $scope.dataBase.data6,
        $scope.dataBase.data7,
        $scope.dataBase.data8,
        $scope.dataBase.data9,
        $scope.dataBase.data10,
        $scope.dataBase.data11,
        $scope.dataBase.data12]
    };

0

Try to put [Frombody] pro parameter to be passed in the Request body:

public HttpResponseMessage Gerar([FromBody]Classe listProdutos)
  • Even using [Frombody], Listacompra Venda and Listameses comes with null value.

Browser other questions tagged

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