Ajax function for MVC controller

3

I have this function with array and I am not able to pass to the controller values, I listing, it appears correctly, I use angular.

  $scope.addItem = function (user) {
        $scope.items.push({
            de: $scope.total,
            ate: user.ate,
            tipo: user.tipo,
            valor: user.valor,

        });
        $scope.total = ''
        $scope.total = user.ate;
        user.ate = '';
        user.tipo = '';
        user.valor = '';
    };

 $scope.gravaItem = function () {
        $.ajax({
            type: 'GET',
            url: '/TaxaPreco/SalvarItens',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify($scope.items),
            success: function (result) {
                console.log('Data received: ');
                console.log(result);
            }
        })
    };

And here are the result when I give console.log($scope.items) ;

Array(1)
0
:
{de: "00:01", ate: "00:29", tipo: "0", valor: "5", $$hashKey: "object:3"}

How can I pass these values to the controller ? And here's how I'm trying to get into the controller , but anyway, none worked as it should:

 public async Task<ActionResult> SalvarItens(List<TarifasPrecosItens> nome )
    {
        var item = new TarifasPrecosItens()
        {
            De = nome[0].De,
           //De = itens.De,
           //Ate = itens.Ate,
           //Fracao = itens.,
           //RepetirACada = 0,
           //TipoValor = tipovalor,
           //Valor = valor,
           //TarifasPrecosId = 25,

        };
    
asked by anonymous 06.06.2018 / 20:43

1 answer

0

Friend, change the method from GET to POST, declare your Controller with [HttpPost] and try the following way.

var varArray = new Array();
//CRIE SEU ARRAY AQUI na varArray...

 $scope.gravaItem = function () {
        $.ajax({
            type: 'POST',
            url: '/TaxaPreco/SalvarItens',
            traditional: true,
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: { 'varArray': varArray },
            success: function (result) {
                console.log('Data received: ');
                console.log(result);
            }
        })
    };

no controller

public async Task<ActionResult> SalvarItens(List<TarifasPrecosItens> varArray)

Also check if the array structure is the same, and try to see the error messages in the logs ...

    
06.06.2018 / 22:18