Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding not working for collections in knockoutjs
    primarykey
    data
    text
    <p>I am trying to use knockout.js to show a specification object on the UI. The specification has a name and it has a few parameterInfo rows. Each ParameterInfo row has a ParameterPartNumber and a bunch of SignalInputs. Each SignalInput has just one property called Name. I am able to show the specification name, the parameterInfo rows and ParameterPartNumber but am not able to show the bunch of SignalInput Names that I have even though the SpecificationModel has the values. I am using the following code:</p> <p><strong>HTML code:</strong></p> <pre><code>&lt;div id="specificationHeader"&gt; Name : &lt;input data-bind='value: Name' /&gt; &lt;br /&gt; &lt;br /&gt; &lt;/div&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt; Parameter Part &lt;/th&gt; &lt;th&gt; Signal Inputs &lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody data-bind="foreach: ParameterInfos"&gt; &lt;tr&gt; &lt;td&gt; &lt;input data-bind='value: ParameterPartNumber' /&gt; &lt;/td&gt; &lt;td&gt; &lt;ul data-bind="foreach: SignalInputs"&gt; &lt;li&gt;&lt;span data-bind='text: Name' /&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p><strong>Javascript/Knockout code:</strong></p> <pre><code>&lt;script type="text/javascript"&gt; 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); &lt;/script&gt; </code></pre> <p>When I run the program in debug mode, I can see the following values:</p> <pre><code>var specificationData = '{"Name":"Specification One", "ParameterInfos": [{"ParameterPartNumber":"26-20700-002", "SignalInputs":[{"Name":"Park Brake"},{"Name":"Neutral"}]} ]}'; </code></pre> <p>It's strange because I was able to get an almost similar example working thanks to the answers for the following question:</p> <p><a href="https://stackoverflow.com/questions/9657002/need-to-pass-initial-viewmodel-data-from-asp-net-mvc-to-knockout-js/9658321#9658321">Need to pass initial viewmodel data from ASP.NET MVC to knockout.js</a></p> <p>Still, somehow the binding code is not working. What am I missing?</p> <p><strong>EDIT:</strong></p> <p>Ok, the following lines work:</p> <pre><code>&lt;td data-bind="foreach: SignalInputs"&gt; &lt;ul &gt; &lt;li&gt;&lt;span data-bind='text: Name' /&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/td&gt; </code></pre> <p>But, the following lines don't</p> <pre><code>&lt;td&gt; &lt;ul data-bind="foreach: SignalInputs"&gt; &lt;li&gt;&lt;span data-bind='text: Name' /&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/td&gt; </code></pre> <p>Any idea why? The latter site of lines work in the other stackoverflow example question I cited.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload