How to add html to the page inside a Razor if statement in a foreach?

You need to add @: in front of the html inside the if statements:

@foreach (var item in Model)
{
    if (count % 4 == 0 || totaltCount == 1)
    {
        @:<div class="in-instructor-container">
    }
        <div class="in-instructor">
            <h3>@item.Name</h3>
            @item.Information
        </div>

    if ((count - 1) % 3 == 0 || count == totaltCount) {
        @:</div>
    }

    count++;
}

I found this solution from this stackoverflow question

Leave a Comment