Iterate through collection and print Index and Item in Razor
asp.net-mvc, asp.net-mvc-3, razor
Solution
Try like this:
@foreach (var item in Model.Topics)
{
<div>@Model.Topics.IndexOf(item). @item.Description</div>
}
Problem
I'm having problems with my razor view. I have the following: ``` public ICollection<Topic> Topics public class Topic { public string Description { get; set; } } ``` I want to iterate through the collection and print out the results like this: ``` @foreach (int index in Enumerable.Range(0, Model.Topics.Count())){ <div>@(index). Model.Topics[@(index)].Description"</div> } ``` The problem is that all I get is: ``` 0. Model.Topics[0].Description" 1. Model.Topics[1].Description" ``` I tried all kinds of things but still can't get the description out. What am I doing wrong :-(