Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor `if` does not register closing `}` if there is an opened `<table>` tag?

This code, in a Razor view:

if (true)
{
    <table>
        <tbody>
}

...

if (true)
{
        </tbody>
    </table>
}

does not run. It claims that there is no closing } on the first if() statement, and I'm guessing it's because of the <table> tag.

Is there a way I can work around this to conditionally insert my <table> tags?

like image 571
keeehlan Avatar asked Jul 01 '13 20:07

keeehlan


3 Answers

Try prepending the tags with @::

if (true)
{
    @:<table>
        @:<tbody>
}

...

if (true)
{
        @:</tbody>
    @:</table>
}
like image 175
D Stanley Avatar answered Sep 29 '22 02:09

D Stanley


You can insert it as strings, then they are not parsed as html and doesn't interfer with the syntax:

@Html.Raw(true?"<table><tbody>":"")

@Html.Raw(true?"</tbody></table>":"")
like image 45
Guffa Avatar answered Sep 29 '22 00:09

Guffa


Razor does expect the matching closing tags to be inside of the statement by default.

You can use the special <text> tag to help Razor out:

@if (true) {
    <text>
    <table>
        <tbody>
    </text>
}
like image 26
Fenton Avatar answered Sep 29 '22 00:09

Fenton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!