简体   繁体   中英

No overload for method 'Write' takes 0 arguments, MVC3 Razor

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.

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>             
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM