Randy Minder
Randy Minder

Reputation: 48392

MVC 5 - Create Conditional Razor Output

I have the following Razor code, which is working correctly.

<div>
    <p class="text-info">Tour Durations</p>
    @for (int i = 0; i < Model.AdvertiserTourDuration.Count; i++)
    {
        <span>
            @Model.AdvertiserTourDuration[i].TourDuration.Description
        </span>
    }
</div>

But, I would like to separate each tour duration with a '/' character. But, of course, I don't want to start or end the list with a '/' character. So I thought I would attempt to place the '/' character before the span, but only if i > 0 (because I don't want to start the list with a '/' character). I can't seem to get the code right, because the code I write to do this ends up rendered in the browser.

Upvotes: 0

Views: 86

Answers (1)

Adrian
Adrian

Reputation: 3438

I don't know what you're doing wrong since you did not post the culprit code but give this a shot.

<div>
<p class="text-info">Tour Durations</p>
@for (int i = 0; i < Model.AdvertiserTourDuration.Count; i++)
{
    if (i > 0)
    {
        <text>/</text>
    }
    <span>
        @Model.AdvertiserTourDuration[i].TourDuration.Description
    </span>
}

Upvotes: 1

Related Questions