I need to set an 'input hidden' form element with an array generated from a knockout computed, to send it to the server within the form.
<input type="hidden" name="CourseSubjects" data-bind="value: allSelectedCourseSubjectsIds"/>
in my ViewModel:
self.allSelectedCourseSubjectsIds = ko.computed(function () {
var result = [];
for (var i = 0; i < self.CourseSubjects.length; i++) {
result = result.concat(self.CourseSubjects[i]().selectedCourseSubjects());
}
return result;
},self).extend({ rateLimit: { method: "notifyWhenChangesStop", timeout: 1000 } });
where self.CourseSubjects is an array of observable objects that contains a selectedCourseSubjects' observable array.
But the problem is that the hidden input's value is a string with a comma-separated list of Ids, while what i need is a string representation of the array. I mean, what i get as input hidden is:
<input type="hidden" name="CourseSubjects" value="1,2,3"/>
and what i need is
<input type="hidden" name="CourseSubjects" value="[1,2,3]"/>
Thank you.