11

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:

enter image description here

Any ideas? :) Thanks

Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182
  • For a (better) solution using helper class see this answer: http://stackoverflow.com/questions/24385181/razor-syntax-how-to-conditionally-wrap-some-inner-html – ThdK Jun 05 '15 at 09:32

5 Answers5

26

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>
       }            
   }
ramiramilu
  • 17,044
  • 6
  • 49
  • 66
3

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

Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
2

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>

Jorge Santos Neill
  • 1,635
  • 13
  • 6
  • Explain why this solution is valid, don't just paste the code right here, is important to bring your expertise to the answer, helping the others to solve similar problems. – capcj Sep 08 '17 at 17:46
  • The first solution is functional but the second solutions is optimal – Jorge Santos Neill Sep 08 '17 at 18:06
1

Try this way:

if (i%2 == 0)
{
    @:<div class="row">
}

Or:

if (i%2 == 0)
{
    <text>
        <div class="row">
    </text>
}

Read this article

karaxuna
  • 26,752
  • 13
  • 82
  • 117
0

you are missing a "}" in the line above (line 8, before starting the code you pasted). the code you have pasted here doesnt have any problem.

JC Lizard
  • 1,050
  • 6
  • 17