我使用了kendo标签条,并尝试为模型集的每个项目显示一个标签。
@if (Model.Entities.Count > 1)
{
@(Html.Kendo().TabStrip()
.Name("tabstrip")
.Items(items =>
{
for (int i = 0; i < Model.Entities.Count; i++)
{
items.Add()
.Text(Model.Entities[i].Name)
.Selected(i == 0)
.Content(@<div>
@Model.Entities[i].Name //causes exception
</div>
);
}
})
)
}
标签内容(@ Model.Entities [i] .Name)中的标记会导致异常。
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
有谁知道我做错了什么或如何正确实现它?
答案 0 :(得分:1)
您遇到了outer variable trap。
这对我有用:
@if (Model.Entities.Count > 1)
{
@(Html.Kendo().TabStrip()
.Name("tabstrip")
.Items(items =>
{
for (int i = 0; i < Model.Entities.Count; i++)
{
var i1 = i;
items.Add()
.Text(Model.Entities[i].Name)
.Selected(i == 0)
.Content(@<div>
@Model.Entities[i1].Name
</div>
);
}
})
)
}