check for value null on razor syntax

991 Views Asked by At

The following code is failing because propiedadesFormularioDetalle is null. I placed an IF before the block code but still not working, if I remove one of the bracked, then it wont compile

  @if (propiedadesFormularioDetalle != null) { }
                        <div class="panel panel-default">
                            <div class="panel-heading">Propiedades adicionales</div>
                            <div class="panel-body">
                                <dl class="dl-horizontal">
                                    @foreach (KeyValuePair<string, string> propiedad in propiedadesFormularioDetalle)
                                    {

                                        <dt>
                                            @Html.DisplayName(propiedad.Key)
                                        </dt>

                                        <dd>
                                            @Html.DisplayFor(prop => propiedad.Value)
                                        </dd>

                                    }
                                </dl>
                            </div>      
                        }                 
                    </div>
4

There are 4 best solutions below

1
On BEST ANSWER

try this:

 @{ 
if (propiedadesFormularioDetalle != null) { 
                        <div class="panel panel-default">
                            <div class="panel-heading">Propiedades adicionales</div>
                            <div class="panel-body">
                                <dl class="dl-horizontal">
                                   foreach (KeyValuePair<string, string> propiedad in propiedadesFormularioDetalle)
                                    {

                                        <dt>
                                            Html.DisplayName(propiedad.Key)
                                        </dt>

                                        <dd>
                                            Html.DisplayFor(prop => propiedad.Value)
                                        </dd>

                                    }
                                </dl>
                            </div>      
                        }                 
                    </div>
}
0
On

You have closed your if condition and have placed an extra closing curly brace before the last div. This should be correct:

@if (propiedadesFormularioDetalle != null) 
{ //} - remove this closing curly brace
    <div class="panel panel-default">
        <div class="panel-heading">Propiedades adicionales</div>
        <div class="panel-body">
            <dl class="dl-horizontal">
                @foreach (KeyValuePair<string, string> propiedad in propiedadesFormularioDetalle)
                {
                    <dt>
                        @Html.DisplayName(propiedad.Key)
                    </dt>
                    <dd>
                        @Html.DisplayFor(prop => propiedad.Value)
                    </dd>
                }
            </dl>
        </div>   
    //} - this needs to be moved to the bottom   
    </div>
}
0
On

Try this:

@if (propiedadesFormularioDetalle != null) 
{ 
     <div class="panel panel-default">
         <div class="panel-heading">Propiedades adicionales</div>
             <div class="panel-body">
                 <dl class="dl-horizontal">
                      @foreach (KeyValuePair<string, string> propiedad in propiedadesFormularioDetalle)
                      {
                           <dt>
                                @Html.DisplayName(propiedad.Key)
                           </dt>
                           <dd>
                                @Html.DisplayFor(prop => propiedad.Value)
                           </dd>

                      }
                  </dl>
              </div>                        
      </div>
} 
0
On
@if (propiedadesFormularioDetalle != null) 
{ 
     <div class="panel panel-default">
          <div class="panel-heading">Propiedades adicionales</div>
          <div class="panel-body">
               <dl class="dl-horizontal">
               @foreach (KeyValuePair<string, string> propiedad in propiedadesFormularioDetalle)
               {
                     <dt>@Html.DisplayName(propiedad.Key)</dt>
                     <dd>@Html.DisplayFor(prop => propiedad.Value)</dd>
                }
                </dl>
           </div>      
    </div>
 }

HOWEVER! Whatever is above the "if" statement can affect this. If you are already in a code-block, Razor will complain about the unecessary "@" in front of the "if" statement.