ViewModel:
public class Foo {
IEnumerable<Bar> Bars { get; set; }
Foo模板:
@Html.EditorFor(x => x.Bars)
条形图模板:
//this is the closest I could find
@ViewData.TemplateInfo.HtmlFieldPrefix //equals "Bars[0]" on the first iteration
在渲染过程中,是否有方法获取模板内当前迭代的计数?除了将HtmlFieldPrefix字符串解析为计数之外。
你能重组你的视图,让它做这样的事情吗:
@for( int idx = 0; idx < Model.Count; idx++ )
{
@Html.EditorFor(m=>m[idx])
}
如果IEnumerable<>不支持Count,但如果不支持,您可以使用其他支持的集合。
在这里分享一下,因为这个问题对我很有用,UIHint
和模板也对我有帮助。
我坚持不触摸Foo模板,但计数应该显示在Bar模板中。这就是CCD_ 2处理CCD_。
- 我将使用
ICollection
,因为EF不支持IEnumerable
进行延迟加载。ICollection
具有Count
性质 - 即使我需要使用
IEnumerable
。我将遍历集合并执行i++
。稍后,在迭代或foreach完成后,i
是总数