问题描述
在ASP.NET MVC中,是否有一种方法在使用EditorTemplates时获得循环索引?过去,当我需要了解模型中元素的索引时,我放弃了使用EditorTemplates,而在基础视图中使用了for循环.我想知道是否有办法在仍使用EditorTemplates的情况下获取元素的索引.
In ASP.NET MVC, is there a way to get the loop index when using EditorTemplates? In the past, when I need to know the index of an element in the model, I forgo using EditorTemplates in favor of a for loop in the base view. I am wondering if there is a way to get the index of an element while still using EditorTemplates.
我的for循环示例:
@{int contentIndex = 0;}
@foreach (var item in Model.Content)
{
<p id="content@(contentIndex)">
@Html.TextArea("Content["+contentIndex+"]", item)
</p>
contentIndex++;
}
看看我如何使用contentIndex
作为段落ID?我希望能够使用EditorTemplate而不是for循环来做到这一点.这可能吗?
See how I use the contentIndex
for the paragraph id? I want to be able to do that using an EditorTemplate instead of a for loop. Is this possible?
推荐答案
Phil Haack撰写了一篇不错的博客文章:
Phil Haack wrote up a nice blog post:
http://haacked.com/存档/2011/04/14/a-better-razor-foreach-loop.aspx
这篇关于在ASP.NET MVC中,是否有一种方法在使用EditorTemplates时获得循环索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!