Reputation: 1955
The following loop is in the view of my MVC project.
@{for (int i=1;i <= 5;i++){
@{foreach(Infographic.Category item in (Model.CatList as List<Infographic.Category>)){
<div id="cat-name-@i">
@item.Name
</div>
<div id="cat-image-@i">
@item.Image
</div>
<div id="cat-percent-savings-@i">
@item.PercentSavings
</div>
<div id="cat-total-spending-@i">
@item.PercentTotalSpending
</div>
<div id="cat-avg-savings-same-store-@i">
@item.AvgPercentSavingsSameStoreShopper
</div>
}
}
}
}
When I run, I get the error: "No overload for method 'Write' takes 0 arguments" highlighted around my foreach
loop.
When I put the for loop inside the foreach, it works but loops each category list 5 times before moving on to the next and gives me 125 items when I only need 25.
Upvotes: 0
Views: 1645
Reputation: 813
You seem to have an unnecessary number of {
s here. The following seems to work on my machine:
@for (int i = 1; i <= 5; i++)
{
foreach (Consumerology.Models.Infographic.Category item in (Model.CatList as List<Consumerology.Models.Infographic.Category>))
{
<div id="cat-name-@i">
@item.Name
</div>
<div id="cat-image-@i">
@item.Image
</div>
<div id="cat-percent-savings-@i">
@item.PercentSavings
</div>
<div id="cat-total-spending-@i">
@item.PercentTotalSpending
</div>
<div id="cat-avg-savings-same-store-@i">
@item.AvgPercentSavingsSameStoreShopper
</div>
}
}
Upvotes: 1