Ive got a mega annoying problem I have a view with:
@{
if(ViewBag.Section == "Home")
{
<div id="headerfrontPage">
}
else
{
<div id="header">
}
}
And I get a compilation error:
The code 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.
How do I conditionally write a div? Its for a hack bascially...
You could try this:
I'm not sure if that will help, it a long shot. But at least imo that looks better...
You can use the same construct when you wrap your div's inside element like:
Or you use razor syntax @: like
But for your current situation I would prefer Ron Sijm's solution:
I suspect it is because your divs are not closed, so razor assumes that the closing brace is actually part of the div content.
You might try outputting the entire div content within your code there, including the closing tag, or output the div tag with a Response.Write, or something similar, so there is no confusing markup.
EDIT: also, maybe enclosing your div tag in a
might be worth a try.
The simplest way to write this would be:
Or, if you prefer, you can use a local variable:
Regarding the more general case of unclosed tags in Razor code blocks, you can explicitly mark the opening tag as content:
Try this: