I am using MVC to display a table of checkbox options. I know that I can call EditorFor()
on a collection of objects and it will produce a list of them by looping through the collection and outputting HTML based on each item's editor template. My question is, is there a way of accessing the iterator of this loop within the editor template, so that I can start a new row of the table every, say, 3 columns?
Asked
Active
Viewed 1,611 times
1

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

Bondolin
- 2,793
- 7
- 34
- 62
3 Answers
1
Additionally, naming the items the correct thing for your model binding is important. See this article:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Ben Finkel
- 4,753
- 7
- 34
- 49
-
Excellent, thanks. I especially found helpful the part describing naming the complex object array elements. – Bondolin Oct 26 '12 at 20:51
0
Follow the Brad Wilson link in the following post, should be what you are after.
How to create custom editor/display templates in ASP.NET MVC 3?
-
thanks for the reply, but I already have a template; I'm trying to get it to work with the iterator from the automatic MVC loop started by using an EditorFor a collection. – Bondolin Oct 26 '12 at 19:48
0
The simplest you can use:
List<string> items = new List<string>();
items.Add("Item 1");
items.Add("Item 2");
items.Add("Item 3");
var result = items.Select((item, index) => new { index, item });
and then if(index % 3 == 0) { ... }
OR
In ASP.NET MVC, is there a way to get the loop index when using EditorTemplates?

Community
- 1
- 1

webdeveloper
- 17,174
- 3
- 48
- 47