1
votes

Need help with bidnings in Knockout. Im totaly newbiew on knockout. Have tried to toubleshoot by firebug without result.

Here is my example

This is the Knockout ViewModel

function MarketViewModel() {
    var self = this;
    self.markets = ko.observableArray();

    var baseUri = 'api/market';

    $.getJSON(baseUri, self.markets);
}

$(document).ready(function () {
    ko.applyBindings(new MarketViewModel());
})

The API controller that returning the array as json works perfectly btw.

And my view

<!-- Default datatable -->
<div class="block well">
    <div class="navbar">
        <div class="navbar-inner">
            <h5>Default datatable</h5>
        </div>
    </div>
    <div class="table-overflow">
        <table class="table table-striped" id="data-table">
            <thead>
                <tr>
                    <th>Marknad</th>
                </tr>
            </thead>
            <tbody data-bind="forech: markets">
                <tr>
                    <td data-bind="text: description"></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<!-- /default datatable -->

@section scripts
{
    <script src="~/Scripts/vm/MarketViewModel.js"></script>
}

UPDATE

Error from firebug: Error: Unable to parse bindings. Message: ReferenceError: description is not defined; Bindings value: text: description This throws Unable to parse binding on the property "description" in view. But the JSON result contains properties for description.

Json array:

[{"$id":"1","MarketId":14,"Description":"SE Projektutveckling","ChUser":"anders.persson","ChTime":"2013-01-28T09:07:50.067"},{"$id":"2","MarketId":39,"Description":"SE Styckehus","ChUser":"anders.persson","ChTime":"2013-01-21T23:01:04.637"},{"$id":"3","MarketId":40,"Description":"NO Styckehus","ChUser":"anders.persson","ChTime":"2013-01-21T23:00:58.01"},{"$id":"4","MarketId":41,"Description":"GB Styckehus","ChUser":"anders.persson","ChTime":"2013-01-21T23:00:50.933"},{"$id":"5","MarketId":42,"Description":"DE Styckehus","ChUser":"anders.persson","ChTime":"2013-02-05T09:39:57.03"},{"$id":"6","MarketId":43,"Description":"SE Fritidshus","ChUser":"anders.persson","ChTime":"2013-01-21T23:00:24.597"}]
1
Can you maybe post a sample json (what is printed out with: $.getJSON(baseUri, function(data) { console.log(data);self.markets(data)}); )? Also there is typo in your data-bind="forech: markets" it should be data-bind="foreach: markets". Also the casing of the properties matters make sure that the result contains the property description and not Description - nemesv
Please se the updated message. Thx! - Nils Anders

1 Answers

1
votes

The property names in the bindings (and in generally in JavaScript) are case sensitive.

In your JSON you have have "Description" with a capital D so you need to write "Description" also in your binding:

<td data-bind="text: Description"></td>