I want to pass an array of strings from my view model (in form of an ko.observableArray) to the controller in Asp.net MVC.
As the ko.observableArray is an object rather than array, it cannot simply be passed through the $.ajax method and used as array in controller side.
How can I pass the data in ko.observableArray to controller so that I can use it as array on the controller side?
Knockout has two utility function called
ko.toJSandko.toJSON.var myPlainObject = ko.toJS(root)will traverse your object and turn any observable into a plain JavaScript property.var myJSONstring = ko.toJSON(root)will do the same and then do aJSON.stringifyon the result.So, you can use those functions on your viewModel to get plain JavaScript objects.
Info from the docs here.
If the items in your observableArray do not contain observables, then you could simply just retrieve the underlying array by doing
myObservableArray()Update: based on comment. This works fine for me:
Against an action like:
Does this match what you are trying?