0

I'm trying to get into razor web pages and have this snippet of code which I can't work out how to get going successfully in razor. The for loop works correctly, but as soon as I add the if statement, it breaks.

                @{
                List<Plant> plants = PlantTableAdapter.Get().Where(t => t.featured).ToList();

                for (int i = 0; i < plants.Count; i++) {
                    if (i % 4 == 0) { 
                        <div class="row">
                    }

                    <div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>
                    <div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>
                    <div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>
                    <div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>

                    @*if ((i + 1) % 4 == 0) {
                        </div>
                    }*@
                }
            }

It seems no matter what combination I try, it just won't work. I came across this solution ASP.NET MVC Razor - if inside for for an if inside a for, but I think because the for is already inside a code block, it breaks it.

Help me stack overflow :)

2

2 Answers 2

1

Try escaping the html inside your if's using the @: line markup

@{
    List<Plant> plants = PlantTableAdapter.Get().Where(t => t.featured).ToList();

    for (int i = 0; i < plants.Count; i++)
    {
        if (i%4 == 0)
        {
            @:<div class="row">
        }

        <div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>
        <div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>
        <div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>
        <div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>

        if ((i + 1)%4 == 0)
        {
            @:</div>
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Coding structure in razor is more reliable and less error prone. You placed an opening div in first 'if' and closing in second. What if first and second 'ifs' are not true in single iteration? then there will be an extra opening or closing which makes no sense.

1 Comment

you are absolutely correct, i would have found that out once i corrected the markup, but thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.