【发布时间】:2021-04-25 05:05:55
【问题描述】:
我有一个包含对象列表 myListB() 的表格,这些对象由 knockout foreach 显示,其行数与 myListB().Count 一样多。
<!--ko with: myProperty-->
<table class="table">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<!-- ko foreach: myListB()-->
<tr>
<td data-bind="text: Property1"></td>
<td data-bind="text: Property2"></td>
@await Html.PartialAsync("~/Views/MyView.cshtml", this)
</tr>
<!-- /ko -->
</tbody>
</table>
<!-- /ko -->
数据模型:
class viewmodel{
public myProperty:Foo;
}
class Foo{
public myListB: KnockoutObservableArray<Model1> = ko.observableArray([])
public myPropertyA:string;
}
class Model1{
public Property1:any;
public Property2:any;
}
有没有办法将一个项目从 foreach 传递到部分视图 MyView.cshtml(它将是 Model1 类型的对象),所以我可以在部分视图中将它用于 data-bind 并访问 @ 的值数组myListB()的每一项的987654328@?因为它是用代码编写的, myProperty 由ko with 传递,所以在部分视图中我可以访问整个myListB() 或myPropertyA。用ko with: $data 包裹部分视图没有帮助,我仍然只能访问myPropertyA 和整个myListB()。
我不能在这里摆脱 foreach 和局部视图,它应该保持现在的状态,问题是关于将数组项从 foreach 数据绑定到局部视图的可能性
MyView.cshtml:
<div class="modal"
role="dialog">
<div class="modal-dialog"
role="document">
<div class="modal-content ">
<div class="modal-header">
<h4 class="modal-title">MyTitle</h4>
</div>
<!--ko if: $data!=null -->
<div class="modal-body ">
<span data-bind="text: $data.Property1"></span>
</div>
<!-- /ko -->
</div>
</div>
</div>
【问题讨论】: