我想知道是否可以在 Razor 语法中使用 Knockout data-bind
。
我认为有以下代码:
<tbody data-bind="foreach: relays">
<tr>
<td id="body-button-column">
<button class="btn btn-default btn-sm" title="Remove relay" data-bind="click: $parent.removeRelay">
<i class="glyphicon glyphicon-remove-circle"></i>
</button>
</td>
<td><input type="text" data-bind="value: $index"/></td>
<td>@Html.DropDownList("RelayConfigurations[" + $index + "].Address", Model.Addresses, "Select an Item")</td>
</tr>
</tbody>
我的 View 模型具有以下属性
self.relays = ko.observableArray(@Html.Raw(Json.Encode(Model.RelayConfigurations)));
其中 RelayConfigurations 是模型上的集合对象。
在使用 Razor 语法的行中 @Html.DropDownList
...我想对 $index 值使用 knockout 绑定(bind),但不知道该怎么做。或者如果有可能的话。
如果我将该行中的 $index
替换为硬编码值零, Controller 操作中的模型绑定(bind)将按预期在回发上工作(尽管只是由于硬编码值为零,集合中的第一个中继有任何数据。
最佳答案
当然你可以在 Razor 中使用 knockout :)
我相信您正在寻找的是这个匿名对象方法:
@Html.DropDownList("somelist", Model.List, new { id = "listbox", data_bind = "" })
关于c# - 使用带有 Razor 语法的 knockout 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23375564/