I have a table with the list myListB()
of objects, which are displayed by knockout foreach
making as many rows as 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 -->
Data Model:
class viewmodel{
public myProperty:Foo;
}
class Foo{
public myListB: KnockoutObservableArray<Model1> = ko.observableArray([])
public myPropertyA:string;
}
class Model1{
public Property1:any;
public Property2:any;
}
Is there a way to pass an item from foreach to partial view MyView.cshtml (it would be an object of type Model1
), so I could use it for data-bind
in partial view and access there value of Property1
of each item of array myListB()
? As it's written in code, myProperty
is passed by ko with
, so in partial view I can access either whole myListB()
or myPropertyA
. Wrapping partial view with ko with: $data
doesn't help, I still can access only myPropertyA
and the whole myListB()
.
I can't get rid of foreach and partial view here, it should remain as it is now, the question is about the possibility of data binding an array item from foreach to partial view
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>