shresthaal
shresthaal

Reputation: 685

Pass the knockout model as data in an ajax call

I have a knockout model with 44 observables. User selects value for the observable and clicks on the Submit button. The Submit button executes the StoreInDB. I want to pass all 44 as a json string. I tried to pass ko.toJSON($root, null, 2), but it does not work.

var TestModel = function() {
   self.Feedback1 = ko.observable();
   self.Feedback2 = ko.observable();
   self.Feedback3 = ko.observable();
   .
   .
   .
   self.Feedback44 = ko.observable();

   self.StoreInDB = function() {
                   $.ajax({
                             type:"POST"
                             url: url,
                             data:
                             contentType: "application/json; charset=utf-8",
                             dataType: "json",
                    });
  };

};

Upvotes: 1

Views: 783

Answers (1)

rwisch45
rwisch45

Reputation: 3702

You can use ko.toJSON(self) to get the JSON. See this fiddle, http://jsfiddle.net/rwisch45/u4a3K/

Upvotes: 1

Related Questions