Telerik的MVC - 不加载标签栏内容动态 [英] Telerik MVC - Not Loading Tabstrip Content Dynamically
问题描述
我试图加载其中有一个标签栏的局部视图。我想提出一个jQuery的Ajax调用返回包含标签栏的局部视图控制器。我相信一切都被正确加载JS Telerik的
I am trying to load a partial view which has a tabstrip. I am making a jQuery ajax call to the controller which returns a partial view which contains the tab strip. I believe all the Telerik JS is being loaded properly.
下面是局部视图的部分:
Here are pieces of the partial view:
<% Html.Telerik().TabStrip()
.Name("TabStrip")
.Items(tabstrip =>
{
foreach (FooType foo in Model.Section) {
tabstrip.Add()
.Text(foo.Name.ToString())
.Content(() =>
{%>
<div><%= foo.bar.ToString() %></div>
<%});
}
})
.SelectedIndex(0)
.Render();
%>
的问题是,具有相同内容不管。它总是从Model.Section的最后一个项目。该标签文本输出正确,虽然
The problem is that has the same content no matter what. Its always the last item from Model.Section. The tab text is is outputted correctly though.
我试过与获得在每次迭代递增计数器更换foo.bar.ToString,但片最终都具有相同的价值,最后的计数值。
I've tried replacing the foo.bar.ToString with a counter that get incremented at each iteration, but the tabs end up all having the same value, the last counter value.
我是什么失踪?
推荐答案
这是造成拉姆达一个众所周知的问题,通过引用捕捉富
变量没有值。当标签被渲染富
的最后一个值将被使用。检查这博客文章更详细的解释。
This is a well known problem caused by the lambda capturing the foo
variable by reference not by value. When the tabs are rendered the last value of foo
will be used. Check this blog post for a more detailed explanation.
的解决方案是使用在循环内的局部变量
The solution is to use a local variable inside the loop:
foreach (FooType foo in Model.Section) {
var bar = foo.bar.ToString();
tabstrip.Add()
.Text(foo.Name.ToString())
.Content(() => /* use the local variable in the lambda */
{%>
<div><%= bar %></div>
<%});
}
这篇关于Telerik的MVC - 不加载标签栏内容动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!