user3663854
user3663854

Reputation: 463

Multiple if razor syntax

Need your kind help on the razor syntax below as it is throwing error...

            <div class="container">
                        @{var i = 0;}
                        @foreach (var m in Tests)
                        {
                            if (i%3==0)
                            { 
                                <div class="row">
                            }

                            <div class="col-md-3">ass</div>

                            @if (i % 3 == 1)
                            {
                                </div>
                            }
                            @{i++;}
                        }

                </div>

The using 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.

Then, I tried...

                <div class="container">
                        @{
                            var i = 0;
                            foreach (var m in Tests)
                            {
                                if (i%3==0)
                                { 
                                    <div class="row">
                                }

                                <div class="col-md-3">ass</div>

                                if (i % 3 == 1)
                                {
                                    </div>
                                }
                                i++;
                            }
                        }

but this give me...

ass  }if (i % 3 == 1) { 
ass  }if (i % 3 == 1) { 
ass  }if (i % 3 == 1) {

I appreciate help to correct my code...

Upvotes: 0

Views: 1725

Answers (2)

Christos
Christos

Reputation: 53958

You should remove the @ from this @if (i % 3 == 1) and this @{i++;}. Both the expressions are already in the code block of: @foreach (var m in Tests). Hence you don't need this @.

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239200

Razor cannot properly match opening and closing tags if they are within separate code blocks, and as a result cannot properly parse the view. If you need to do something like this you either need to prefix the tags with @: to tell Razor to ignore parsing tags for that line or wrap them in @Html.Raw:

For example:

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

Or:

if (i % 3==0)
{ 
    @Html.Raw("<div class='row'>")
}

Upvotes: 6

Related Questions