mvc3 razor conditional wrapper div

2019-03-11 15:23发布

问题:

What's the best way to handle something like this:

Razor Code:

@if(!disableRowDiv)
{
    <div class="row">
}

<div>some content here</div>

@if(!disableRowDiv)
{
    </div>
}

So that the Razor engine doesn't produce this error :

Parser Error Message:

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

回答1:

This should work

@if(!disableRowDiv)
{
    @:<div class="row">
}
<div>some content here</div>
@if(!disableRowDiv)
{
    @:</div>
}