模型视图控制器-EditorFor Template的MVC IEnumerable;如何计数



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_。

  1. 我将使用ICollection,因为EF不支持IEnumerable进行延迟加载。ICollection具有Count性质
  2. 即使我需要使用IEnumerable。我将遍历集合并执行i++。稍后,在迭代或foreach完成后,i是总数

最新更新