2012-11-15 33 views
0

我有以下代碼:淘汰賽映射插件失敗的Json集合從ASp.Net的WebAPI結合

/* 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); 

我的HTML看起來像這樣:

<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> 

只要我不知道要在這條線後的foreach:

我真的需要使用映射插件,因爲有我不想代碼在這兩個地方有很多OB對象(敲除和C#中的服務層JS)

的小提琴是這樣的:

http://jsfiddle.net/3Q6JE/

謝謝! !

回答

1

你可以把$data到的foreach:

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

但你json應該是以下幾點:

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

這裏是工作提琴:http://jsfiddle.net/3Q6JE/2/

+0

謝謝,現在它的工作! – Juan