I am trying to use knockout.js to display a specification object in the user interface. The specification has a name and has several parameterInfo lines. Each ParameterInfo line has a ParameterPartNumber parameter and a bunch of SignalInputs. Each SignalInput has only one Name property. I can show the specification name, parameterInfo and ParameterPartNumber strings, but I can not show the bunch of SignalInput names that I have, even if the Model specification has values. I am using the following code:
HTML code:
<div id="specificationHeader"> Name : <input data-bind='value: Name' /> <br /> <br /> </div> <table> <thead> <tr> <th> Parameter Part </th> <th> Signal Inputs </th> </tr> </thead> <tbody data-bind="foreach: ParameterInfos"> <tr> <td> <input data-bind='value: ParameterPartNumber' /> </td> <td> <ul data-bind="foreach: SignalInputs"> <li><span data-bind='text: Name' /></li> </ul> </td> </tr> </tbody> </table>
Javascript / Knockout Code:
<script type="text/javascript"> var SpecificationModel = function (specification) { var self = this; self.Name = ko.observable(specification.Name); self.ParameterInfos = ko.observableArray(ko.utils.arrayMap(specification.ParameterInfos, function (ParameterInfo) { return { ParameterPartNumber: ParameterInfo.ParameterPartNumber, SignalInputs: ko.observableArray(ParameterInfo.SignalInputs) }; })); }; var specificationData = '@Html.Raw(ViewBag.SpecificationData)'; var viewModel = new SpecificationModel($.parseJSON(specificationData)) ko.applyBindings(viewModel); </script>
When I run the program in debug mode, I can see the following values:
var specificationData = '{"Name":"Specification One", "ParameterInfos": [{"ParameterPartNumber":"26-20700-002", "SignalInputs":[{"Name":"Park Brake"},{"Name":"Neutral"}]} ]}';
This is strange, because I was able to get an almost similar example, working thanks to the answers to the following question:
It is necessary to transfer the source data as a viewmodel from ASP.NET MVC to knockout.js
However, the binding code does not work. What am I missing?
EDIT:
Well, the following lines work:
<td data-bind="foreach: SignalInputs"> <ul > <li><span data-bind='text: Name' /></li> </ul> </td>
But the following lines are not
<td> <ul data-bind="foreach: SignalInputs"> <li><span data-bind='text: Name' /></li> </ul> </td>
Any idea why? The last line site works in another example stackoverflow example that I cited.