28

I am trying to do this using Razor templating:

@if(isNew)
{
   <div class="new">
}

...


@if(isNew)
{
   </div>
}

The error is:

cannot resolve the symbol 'div'

Razor doesn't like the ending div tag inside the IF clause, how can I get this to work? Is there an escape I have to use?

1 Answer 1

61

Razor doesn't like it when you split start/end tags up like this as it can't parse the HTML properly, so you have to escape them as plain text:

@if(isNew)
{
   @:<div class="new">
}

...


@if(isNew)
{
   @:</div>
}    

The more conventional approach would be to repeat the markup inside the div (using partials or otherwise) - which approach is more desirable, I would say, is dependent upon the nature of the markup in between the conditional divs:

@if(isNew)
{
    <div class="new">
        <!-- some markup or partial view -->
    </div>
}
else
{
    <!-- some markup or partial view -->
}

I would prefer this approach if either:

  1. The contained markup can be usefully contained in a partial such that it is reusable elsewhere.
  2. The conditional wrapping markup is more than a couple of lines, at which point escaping markup becomes messy.

I should also add the option of using helper methods:

@helper MainMarkup()
{
    <!-- some markup or partial view -->
}

@if(isNew)
{
    <div class="new">
        @MainMarkup()
    </div>
}
else
{
    @MainMarkup()
}

This is useful if you want to use the second option above but avoid repeating markup or nesting too many partials (particularly if this markup is only relevant for this view).

8
  • I've come to this problem too, but later I realized I actually did something wrong logically speaking. Although this works I don't think it is the way to go. +1 for the solution anyhow.
    – Silvermind
    Dec 2, 2013 at 21:50
  • 1
    @Silvermind True, this will happen if you have invalid HTML, but it will also happen if you are doing something like the above and inserting a conditional container around some other markup. The alternative is to repeat the inner markup, which is not always convenient (or even tidy), even if you use partials.
    – Ant P
    Dec 2, 2013 at 21:52
  • True too, but I'd rather repeat one line of code if I really must for calling a PartialView/UserControl than get messy with this.
    – Silvermind
    Dec 2, 2013 at 21:54
  • @Silvermind It's a toss-up - for a one-liner like this I'd say using partials is probably just unnecessary abstraction; if the wrapping markup is more complex (or there are other benefits to using partials, re. usability) I would agree with you.
    – Ant P
    Dec 2, 2013 at 21:56
  • 2
    That's my point - if there's a lot of markup in between, you don't want it repeating and it might not make semantic sense to abstract it into a partial (depending on what it is, of course). If that's the case, I would prefer just just escape the container than either repeat the inner markup or wrap it in a partial.
    – Ant P
    Dec 2, 2013 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.