I'm using MVC + Razor and where possible would like to stay in the strong typed world.
我正在使用MVC + Razor,并且尽可能希望留在强大的世界中。
Hence hoping to iterate through my view model using Razor syntax, and data-bind to knockout via the array index.
因此希望使用Razor语法迭代我的视图模型,并通过数组索引将数据绑定到knockout。
Is this doable? All the examples I have seen so far preclude the use of the for-each
template.
这可行吗?到目前为止,我看到的所有示例都排除了使用for-each模板的所有示例。
@Html.TextBoxFor(_ => _[i],new {@readonly = "readonly", data_bind = string.Format("value:[{0}]",i) });
Where the strongly typed model is an array, and the above is being iterated over from inside a for loop.
强类型模型是一个数组,上面是从for循环中迭代的。
1 个解决方案
#1
0
Somehow this works thanks to @nemesv for the original jsfiddle:
不知怎的,这要归功于@nemesv的原始jsfiddle:
<input type="text" data-bind="value: $data[0].name"></input>
<input type="text" data-bind="value: $data[1].name"></input>
<input type="text" data-bind="value: $data[2].name"></input>
With the following javascript file.
使用以下javascript文件。
var ViewModel = ko.observableArray( [{
name: 'name1'
}, {
name: 'name2'
}, {
name: 'name3'
}]
);
ko.applyBindings(ViewModel);
The $data is required without it, it doesn't work. I'm still not sure why this is.
没有它需要$ data,它不起作用。我还不确定为什么会这样。
#1
0
Somehow this works thanks to @nemesv for the original jsfiddle:
不知怎的,这要归功于@nemesv的原始jsfiddle:
<input type="text" data-bind="value: $data[0].name"></input>
<input type="text" data-bind="value: $data[1].name"></input>
<input type="text" data-bind="value: $data[2].name"></input>
With the following javascript file.
使用以下javascript文件。
var ViewModel = ko.observableArray( [{
name: 'name1'
}, {
name: 'name2'
}, {
name: 'name3'
}]
);
ko.applyBindings(ViewModel);
The $data is required without it, it doesn't work. I'm still not sure why this is.
没有它需要$ data,它不起作用。我还不确定为什么会这样。