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 :-(
Try:
Or even better:
This should work:
What your doing is trying to use the
foreach
like a for loop. (Possibly like a C++ iterator?) The foreach is however syntactic sugar that does all that work for you.In C# foreach loops over typed collections. So if you have :
The loops would be:
Works for anything IEnumerable (which is IList, Arrays, Collections etc)
Try like this: