How to insert arrays inside angular objects?

Asked

Viewed 416 times

1

Let’s say I have a vehicle object that has the following attributes converting my class c# to Json:

 {
  "UsuarioId": 0,
  "TipoId": 0,
  "MarcaId": 0,
  "ModeloId": 0,
  "VersaoId": 0,
  "Quilometragem": 0,
  "AnoFabricacao": 0,
  "AnoModelo": 0,
  "Combustivel": null,
  "Cor": null,
  "Placa": null,
  "VsCustom": null,
  "Valor": 0.0,
  "Troca": false,
  "inPublico": false
}

But this vehicle will also have photos and videos of which I use ng-file-upload and convert to Base64. In addition this vehicle has optional items that are loaded into an ng-repeat with checkboxes where the user will select the optional items. So far, so good! The same problem is at the time of the post. If I do everything separately it works, but I would like to insert the photo array and item array within the object. I’ve tried to send the object this way:

$scope.postVeiculo = function () {
    var v = {
        Marca: $scope.veiculo.Marca,
        Modelo: $scope.veiculo.Modelo,
        Versao: $scope.veiculo.Versao,
        ....
        Files: [$scope.files], <- Esse é o array de fotos
        Items: [$scope.items] <-- esse é o de itens opcionais
    };

    $http({
        method: 'POST',
        url: 'minhaUrl',
        data: $.param(v),
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    })
    .success(function (data, status) {
        ...tratamento se ok
    })
    .error(function (data, status) {
        ...tratamento do erro
    });
};

I have done several attempts with push, splice, but Sponse always returns me Reference not set to object instance. Sometimes it is frustrating to work with angular and ASP.NET MVC projects and the documentation is not very enlightening. I thank anyone who can help.

  • If you are returning a C# error you better debug there and see the cause of the error.

1 answer

4


For example, simply remove the brackets:

$scope.postVeiculo = function () {
    var v = {
        Marca: $scope.veiculo.Marca,
        Modelo: $scope.veiculo.Modelo,
        Versao: $scope.veiculo.Versao,
        ....
        Files: $scope.files, //<-- Se $scope.files já um array, não precisa de colchetes
        Items: $scope.items //<-- Se $scope.items já um array, não precisa de colchetes
    };
  • Thanks Thiago, I solved the problem by initializing the object $Scope.new = { User: null, Tipoid: null, Marcaid: null, Modeloid: null, Versaoid: null, Mileage: null, Anomanufacture: null, Anomodel: null, Fuel: null, Colour: null, Card: null, Vscustom: null, Value: null, Exchange: false, inPublico: false, Files: [], Items: [] }; that’s how it worked!

Browser other questions tagged

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