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?
Try prepending the tags with @:
:
if (true)
{
@:<table>
@:<tbody>
}
...
if (true)
{
@:</tbody>
@:</table>
}
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>":"")
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>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With