简体   繁体   中英

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() ))  

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>
}

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM