I am using a foreach template with knock out.
I have a binding in my page
<div data-bind="template: { name:'myList', foreach: viewModel.myList}</div>
In my template, I have an attr binding on an input:
data-bind="attr: { value: $data.Desc }"
viewModel.myList.push(listItem);
I then convert my view model to JSON:
var json = ko.mapping.toJSON(viewModel);
The value in the input is not bound to the observable array. Even if I hard code the value like below it doesn't bind.
data-bind="attr: { value: 1 }"
How do I keep my view model observable array in sync with my input when adding?
More Info - Update
I am populating my initial view model using: viewModel.myList = ko.mapping.fromJS(model);
model is passed to my JavaScript using @Html.Raw(JsonConvert.SerializeObject(Model), which is passed from an MVC controller. It contains a list of objects called 'myList'.
I'm not sure exactly what else to provide (sorry I can't provide a fiddle at the moment due to the dependency on my DB, MVC etc). Does this help answer?
Solution?
It seems I cannot use the attr binding with value - if I move the value binding out side like this:
data-bind="value: $data.Desc, attr: { other attributes here... }
it works. Anyone know why?