Ajax function for MVC controller

Asked

Viewed 144 times

3

I have this function with array and I’m not getting through to the controller values, i listing, it appears correctly, 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 results when I give one 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 on to the controller? And here’s how I’m trying to get controller, but in any case, 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,

        };
  • Doubt: if nome is a list of TarifasPrecosItens, then why create another object TarifasPrecosItens with the data? It would not be enough to do something like var item = nome[0]?

  • I need it to receive the array to perform the Inserts on the controller page, and this is not happening.

  • I updated the question with the angle that picks up the data, so you can understand better.

1 answer

0

Friend, change the GET method to POST, declare your Controller with [Httppost] and try it 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);
            }
        })
    };

in the controller

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

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

  • I didn’t understand the part of creating the array, and I took the function at the angular, which already has the array.

  • I updated the question with the angle I take the data.

Browser other questions tagged

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