淘汰赛MVC与现有的C#code - 循环 [英] Knockout MVC with existing C# code - looping
问题描述
在code在我看来 -
The code in my view -
@{ int tabIndex = 0; }
@using (var metricGroup = ko.Foreach(m => m.MetricGroups))
{
tabIndex++;
<div id="tabs-@tabIndex" > ... </div>
}
这里的问题是所有的div渲染时,id是始终不变 - 标签-1
代替的是,它应该是标签-1,标签-2,标签-3...
The issue here is for all the divs rendered, the id is always the same - "tabs-1" instead of that, it should be "tabs-1", "tabs-2", "tabs-3" ...
任何人都可以请帮我这个问题?
我有一个非常复杂的看法和淘汰赛MVC是快把我逼疯了。
Can anyone please help me out with this issue? I have a highly complex view and Knockout MVC is driving me crazy
推荐答案
由于的 ko.Foreach
你的HTML将在客户端生成STRONG>,因此设置你的div的剃刀ID,因为剃刀code是服务器端执行不工作。
Because of the ko.Foreach
your HTML will be generated on the client side, so setting the your div's id with Razor does not work because the Razor code is executed on server side.
您需要做的是使用 $指数产生在客户端的ID与敲除()
的结合上下文属性和的 ATTR
绑定:
What you need to do is to generate the id's on the client side with Knockout using the $index()
binding context property, and the attr
binding:
@using (var metricGroup = ko.Foreach(m => m.MetricGroups))
{
<div data-bind="attr: { id: 'tabs-' + $index() }" > ... </div>
}
淘汰赛-MVC的 ko.Bind.Attr
方法不使用 GetIndex()
方法,以便您工作需要写出来的这种淘汰赛手工装订前pression。
The Knockout-Mvc's ko.Bind.Attr
method is not working with the GetIndex()
method so you need to write out the this Knockout binding expression by hand.
这篇关于淘汰赛MVC与现有的C#code - 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!