Jed Grant
Jed Grant

Reputation: 1425

Wrap part of a foreach loop in a div

I am trying to wrap everything after the second item in a doc. Here's the code that doesn't work. It's being used in a Razor view.

@{var j = 0; var count = 10;}
@foreach (var reply in comment.CommentReply.OrderBy(x => x.DateCreated))
{

    if (j == 2 && count > 2) {
        <div class="hidden-replies">
    } 

    Html.RenderPartial("_Reply", reply);

    j++;
    if (j == count && count > 2) {
        </div>
    }

}

I believe it's still waiting for the closing div so the else doesn't get read. How do I fix this?

Upvotes: 2

Views: 1227

Answers (2)

Devin Goble
Devin Goble

Reputation: 2857

I misunderstood the original question. Disregard the following:

Try something like this instead:

@{var j = 0; var count = 10;}
@foreach (var item in replies)
{    
    if(j <= 2)
    {
        Html.RenderPartial("_Reply", item);
    }

    if (j == 3) {
        <div class="hidden-replies">
    }

    j++;

    if (j == count) {
        </div>
    }
}

Upvotes: 0

SLaks
SLaks

Reputation: 887449

You need to prefix the half-tags with @: to prevent Razor from parsing the HTML.

Upvotes: 7

Related Questions