How to insert arrays inside angular objects?

1

Let's say I have a vehicle object that has the following attributes converting my C # class 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. Until then, okay! The problem itself is at the time of the post. If I do everything separate works, but I would like to insert the array of photos and array of items within the object. I've already 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 tried several times with push, splice, but the response always returns me a reference not set to an object instance. It's sometimes frustrating to work with angular and ASP.NET MVC projects and the documentation is not very enlightening there. Thanks for helping.

    
asked by anonymous 26.01.2017 / 02:14

1 answer

4

According to your example, just 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
    };
    
26.01.2017 / 09:10