Luiz Carlos Lucena
Luiz Carlos Lucena

Reputation: 135

Razor + Javascript weird error

I saw a very very weird error at my razor codes see the following code:

<script type="text/javascript">
@if (Model.SomeVerification)
{
   <text>
   if (startHour.getTime() >= maxStartHour.getTime() || startHour.getTime() < maxStartHour.getTime()) {
      if (!(checkIN.valueOf() <= currentNow.setDate(now.getDate() + 1).valueOf())) {
      //The code goes on...
      }
   }
  </text>
}
</script>

The code syntax is just perfect but when I try to run it, I have the following error:

Message Parser Error: End of file reached or unexpected character before the "" mark could be analyzed. Elements within blocks of markup should be complete. They must be self-closing ("
") or have corresponding end tags ("

Hello "). If you wanted to display a "<" character, use the "<" HTML entity.

The trouble was the "<" at "<=", but I didn't understand why since I have other blocks with "<" alone, without the "=". Does anyone have seen that before and solved it with a better solution than mine? I've solved just changing the if position:

if (!(currentNow.setDate(now.getDate() + 1).valueOf() >= checkIN.valueOf() ))  

Upvotes: 2

Views: 609

Answers (3)

tedebus
tedebus

Reputation: 1050

To "cheat" Razor just add a comment at the end of the line with the opposite parenthesis:

if (!(checkIN.valueOf() <= currentNow.setDate(now.getDate() + 1).valueOf())) { //>

Upvotes: 0

Tom Faltesek
Tom Faltesek

Reputation: 2818

What if you try placing the script tags inside your razor if block instead of using the text tag?

Example:

@if (Model.SomeVerification)
{
    <script type="text/javascript">
       if (startHour.getTime() >= maxStartHour.getTime() || startHour.getTime() < maxStartHour.getTime()) {
          if (!(checkIN.valueOf() <= currentNow.setDate(now.getDate() + 1).valueOf())) {
          //The code goes on...
          }
       }
    </script>
}

Upvotes: 3

Ricky Stam
Ricky Stam

Reputation: 2126

You need yo use the @: Syntax to tell Razor to parse each if statement as text. If you had only one if statement the text tag would work but since you have two if statements nested it's better to use @: syntax.

<script type="text/javascript">
@if (Model.SomeVerification)
{

   @:if (startHour.getTime() >= maxStartHour.getTime() || startHour.getTime() < maxStartHour.getTime()) {
      @:if (!(checkIN.valueOf() <= currentNow.setDate(now.getDate() + 1).valueOf())) {
      //The code goes on...
      }
   }

}
</script>

Upvotes: 0

Related Questions