Razor if / else inside a for loop: The for block is missing a closing "}" character
asp.net-mvc, asp.net-mvc-4, razor
Solution
This is a working solution, I tested in Visual Studio on a Razor View. You have to use @: properly.
@for (int i = 0; i < Model.ProductViewModels.Count; i++)
{
if (i % 2 == 0)
{
@:<div class="row">
}
<div class="col-md-4">
<a href="/product?id=@Model.ProductViewModels[i].Id">@Model.ProductViewModels[i].Title - @Model.ProductViewModels[i].Isbn13
<br />
<img src="@Model.ProductViewModels[i].ImageUrl" />
</a>
</div>
if (i % 2 == 0)
{
@:</div>
}
}
Problem
I am doing a simple for loop in Razor syntax in MVC: ``` @for (int i = 0; i < Model.ProductViewModels.Count; i++) { if (i%2 == 0) { <div class="row"> } <div class="col-md-4"> <a href="/product?id=@Model.ProductViewModels[i].Id">@Model.ProductViewModels[i].Title - @Model.ProductViewModels[i].Isbn13 <br /> <img src="@Model.ProductViewModels[i].ImageUrl" /> </a> </div> @if (i%2 == 0) { </div> } } ``` This seems like pretty legal code in my mine mind, but it isn't working! I get the error: ``` Meddelelse om parserfejl: The for block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. ``` Screenshot of error: Any ideas? :) Thanks