MVC View Conditions

1

I need my program to do the following flow control, but the else does not detect it, it's as if it was not written.

Code:

 <table class="table">
               <thead>
                   <tr>
                       <th>Remitente</th>
                       <th>Asunto</th>
                       <th>Recibido</th>
                   </tr>
               </thead>
               <tbody>

                   @foreach (var correo in Model)
                   {
                       DateTime horaRecibida = @correo.recibido;
                       DateTime horaActual = DateTime.Now;

                       Double minutos = horaRecibida.Subtract(horaActual).TotalMinutes;

                       if (minutos > 3)
                       {
                 <tr style="background-color:red">
                       <td>@correo.remitente</td>
                       <td>@correo.asunto</td>
                       <td>@correo.recibido</td>
                   </tr>
                        }

                      else
                       {
                        <tr>
                      <td>@correo.remitente</td>
                      <td>@correo.asunto</td>
                      <td>@correo.recibido</td>
                        </tr>
                       }
                   }       

               </tbody>
           </table>

Error:

  

Compiler error message: CS1513:} expected

This error jumps into a self-generated class, if I remove the flow control from my .cshtml class there is no problem.

    
asked by Hector Lopez 24.01.2018 в 12:10
source

4 answers

3

It has cost me but I have managed to reproduce the problem.

It seems like a problem with the Razor engine because there is an error but the code generated in the self-generated class is strange: it removes the } final from foreach .

Actually the problem is in the instruction:

DateTime horaRecibida = @correo.recibido;

Where you have plenty of @

    
answered by 24.01.2018 / 13:08
source
1

This is the corrected code:
I added Math.Abs to get the minutes, the main problem was that you closed the for each before time and the @ left over in mail. Received.

                       @foreach (var correo in Model)
                       {

                           DateTime horaRecibida = correo.recibido;
                           DateTime horaActal = DateTime.Now;

                           Double minutos = Math.Abs(horaRecibida.Subtract(horaActal).TotalMinutes);


                           if (minutos > 10)
                           {
                   <tr style="background-color:red">
                       <td>@correo.remitente</td>1
                       <td>@correo.asunto</td>
                       <td>@correo.recibido</td>
                   </tr>

                           }
                           if (minutos < 10)
                           {
                   <tr>
                       <td>@correo.remitente</td>
                       <td>@correo.asunto</td>
                       <td>@correo.recibido</td>
                   </tr>
                           }
                       }
    
answered by 24.01.2018 в 13:12
0

I would say that you have the parentheses of the else :

 if(condition)
 {
      ...
 }
 else
 {
      ...
 }
    
answered by 24.01.2018 в 12:28
0

I think you should only use one @ in that if statement. Your code would be as follows:

@foreach (var correo in Model)
{
       DateTime horaRecibida = @correo.recibido;
       DateTime horaActal = DateTime.Now;

       Double minutos = horaRecibida.Subtract(horaRecibida).TotalMinutes;

       @if (minutos > 3)
       {
           <tr style="background-color:red">
           <td>@correo.remitente</td>
           <td>@correo.asunto</td>
           <td>@correo.recibido</td>
           </tr>
       }
       else
       { 
           <tr>
           <td>@correo.remitente</td>
           <td>@correo.asunto</td>
           <td>@correo.recibido</td>
           </tr>
       }
}                     
    
answered by 24.01.2018 в 12:33