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,596 times
1
Brian Tompsett - 汤莱恩
- 5,438
- 68
- 55
- 126
Bondolin
- 2,536
- 6
- 30
- 59
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,693
- 6
- 32
- 48
-
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
- 16,936
- 3
- 47
- 47