0

How to send Array of models to Post API method in backbone?

I am using backbone.js in my mvc application and I have a scenario where I have to pass an array to my Post method in Rest API . I am trying to send an array of models and then calling this.collection.create(model). I am d trying to call Post method as

     var ModelArray = Backbone.Model.extend({
                            urlRoot: av.api.patientInsuranceAddress,
                            toJSON: function () {
                                return this.collection.toJSON();
                            }
                        });

                        var modelCollection = new ModelArray(
                            {
                                collection: this.collection.models
                            });

                        modelCollection.save();

Here e.models is an array of models which I convert to json and call Post method and I want to recieve array of models on Post method like this

public HttpResponseMessage Post(InsuranceAddressViewModel[] model)
{
return null;
}

But I am getting null array in Post method. Is my method of converting array of models to json is fine or will have to do something else. I have tried couple of solutions but couldn't get idea. Please guide

I am trying this solution

When controls comes to toJSON in model. it is giving Uncaught RangeError: Maximum call stack size exceeded. Please guide


    I am posting my code here 

       var PayerAddress = Backbone.Model.extend({
       urlRoot: av.api.patientInsuranceAddress,
       defaults: {
        Address: '',
        City: '',
        State: '',
        Zip: ''
     },
      toJSON: function () {
         return this.collection.toJSON();
      }

                     });

        var Payers = Backbone.Collection.extend({
       //url: av.api.patientInsuranceAddress,
     model: PayerAddress,

 });

       "Done": {
                    class: 'btn',
                    text: "Done",
                    click: function () {
                        payerName = self.$el.find('#Payer').val();

                        var ModelArray = Backbone.Model.extend({
                            urlRoot: av.api.patientInsuranceAddress,
                            toJSON: function () {
                                return this.collection.toJSON();
                            }
                        });

                        var modelCollection = new ModelArray(
                            {
                                collection: self.collection.models
                            });

                        modelCollection.save();
                        //self.closedialog();
                        $(this).dialog("close");
                    }
                }
Community
  • 1
  • 1
touseefkhan4pk
  • 473
  • 4
  • 26

1 Answers1

1

Are you trying to add models to an existing collection or are you trying to replace your collection with a bunch of models? If the latter use:

this.collection.reset(e.models);

The following this.collection.create(myArrayofModels ); 'is used to create a new instance of a model within a collection.' In your above example, you are pasting a string. Backbone then tries to add that string as a model into your collection.

TYRONEMICHAEL
  • 4,174
  • 4
  • 30
  • 47
  • i have edited my questions and the problem I am facing. I coudln't get it solved. plz guide someone – touseefkhan4pk Dec 12 '12 at 08:42
  • How are you populating 'modelCollection'? Where is self.collection.models coming from? Are you using chrome? If so, in your dev tools, go to network > click the post call > click headers and copy the request payload. Paste it here so we can take a look at it. – TYRONEMICHAEL Dec 12 '12 at 09:43
  • I am adding my collection to my model here. I am defining a model property collection in while creating my model instance and populating that collection property with the arrays of models(this.collection.models) I have. Actualy this.collection is my my collection which is populated with array of models which I want to post to the server. So Guide? – touseefkhan4pk Dec 12 '12 at 12:25
  • Thank very much! I've been creating models over and over. What i needed is adding them to an existing collection – Dany Dec 27 '19 at 09:35