c# - Render dictionary with foreach -
c# - Render dictionary with foreach -
i've got dictionary has been serialized webapi2 key object name , value value.
how can create knockout render it's foreach binding?
i tried utilize $data[0]
key , $data[1]
value, didn't work.
<table id="context-data" class="table-striped properties"> <thead> </thead> <tbody data-bind="foreach: properties"> <tr> <th data-bind="text: $data[0]" style="text-align: right"></th> <td data-bind="text: $data[1]"></td> </tr> </tbody> </table>
the foreach
binding needs array , can utilize object.keys
method array of given object's own enumerable properties.
then can utilize $data
display property name , array indexer syntax on properties
object value (properties[$data]
):
<tbody data-bind="foreach: object.keys(properties)"> <tr> <th data-bind="text: $data" style="text-align: right"></th> <td data-bind="text: $parent.properties[$data]"></td> </tr> </tbody>
note: need utilize $parent
access properties
within foreach.
demo jsfiddle.
c# knockout.js asp.net-web-api2
Comments
Post a Comment