0

I have the following code:

/*   Sample data received from an ASP.Net WebApi ajax call  */
var data = { [{"CodSeguro":676541,"NroSeguro":538178},{"CodSeguro":687069,"NroSeguro":577836]},{"CodSeguro":123,"NroSeguro":233]};

/*This function build the view model that will be shared by multiple pages*/
function getViewModel(data)
{
   return ko.mapping.fromJS(data);
}

var viewModel = getViewModel(data);
ko.applyBindings(viewModel);

My Html look like these:

<table>
<thead>
    <tr>
        <th>CodSeguro</th>
        <th>NroSeguro</th>
        <th>NroEndoso</th>        
    </tr>
</thead>
<tbody data-bind="foreach: ">
  <tr>
    <td>
        <span data-bind="text: CodSeguro"></span>
    </td>
    <td>
        <span data-bind="text: NroSeguro"></span>
    </td>
    <td>
        <span data-bind="text: NroEndoso"></span>
    </td>        
</tr>

Simply I dont know what to put after foreach in this line:

I really need to use the Mapping Plugin because there are a lot ob objects that i dont want to code in both places (js for knockout and c# in the service layer)

The fiddle is this:

http://jsfiddle.net/3Q6JE/

Thanks!!!

1 Answer 1

1

You can put $data to foreach:

<tbody data-bind="foreach: $data">

But your json should be the following:

var data = [{"CodSeguro":676541,"NroSeguro":538178},{"CodSeguro":687069,"NroSeguro":577836},{"CodSeguro":123,"NroSeguro":233}];

Here is working fiddle: http://jsfiddle.net/3Q6JE/2/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.