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="/[email protected][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
This solution work for me:
`
@foreach (var item in Model.listItems)
{
if (item.isValid){
@:<div class="validClass">
}else{
@:<div class="invalidClass">
}
@:</div>
}
`
You can try other solution as:
<div class="@if(item.isValid){WriteLiteral("validClass");}" ></div>
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="/[email protected][i].Id">@Model.ProductViewModels[i].Title - @Model.ProductViewModels[i].Isbn13
<br />
<img src="@Model.ProductViewModels[i].ImageUrl" />
</a>
</div>
if (i % 2 == 0)
{
@:</div>
}
}
you need to use text tags
if (i%2 == 0)
{
<text>
<div class="row">
</text>
}
here is a link with more information http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx
Try this way:
if (i%2 == 0)
{
@:<div class="row">
}
Or:
if (i%2 == 0)
{
<text>
<div class="row">
</text>
}
Read this article
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With