Marius Vasile

Marius Vasile

  • 592
  • 1.9k
  • 145.3k

asp.net core razor show div if model has data

Nov 24 2020 11:51 AM
I want to show a div if the asociated model has data after OnPost. What I tried is 
 
 
  1. @if (Model.HazIds != null)  
  2.     {  
  3.         <div id="HazardIdentification">  
  4.             <p class="text-lg-left text-primary pl-1 mt-1 mb-1" style="font-size:24px">Hazard Identification</p>  
  5.             <div class="row no-gutters">  
  6.                 <div class="col-3">  
  7.                     <label for="NameGT" class="form-control badge-primary">Hazard Class</label>  
  8.                 </div>  
  9.                 <div class="col-3">  
  10.                     <label for="EquipmentUsed" class="form-control badge-primary">Hazard Detail</label>  
  11.                 </div>  
  12.                 <div class="col-3">  
  13.                     <label for="Oxygen" class="form-control badge-primary">Hazard Protective Measure Class</label>  
  14.                 </div>  
  15.                 <div class="col-3">  
  16.                     <label for="LEL" class="form-control badge-primary">Hazard Protective Measure Detail</label>  
  17.                 </div>  
  18.             </div>  
  19.             @foreach (var item1 in Model.HazIds)  
  20.             {  
  21.                 <div class="row no-gutters">  
  22.                     <div class="col-3">  
  23.                         <span class="form-control" style="font-size:12px;">@Html.DisplayFor(modelItem => item1.HazClass)</span>  
  24.                     </div>  
  25.                     <div class="col-3">  
  26.                         <span class="form-control" style="font-size:12px;">@Html.DisplayFor(modelItem => item1.HazDetails)</span>  
  27.                     </div>  
  28.                     <div class="col-3">  
  29.                         <span class="form-control" style="font-size:12px;">@Html.DisplayFor(modelItem => item1.HazPMClass)</span>  
  30.                     </div>  
  31.                     <div class="col-3">  
  32.                         <span class="form-control" style="font-size:12px;">@Html.DisplayFor(modelItem => item1.HazPMDetails)</span>  
  33.                     </div>  
  34.                 </div>  
  35.             }  
  36.         </div>  
  37.     }  
  
but is not giving then results expected. If I use !=null it shows the div content (labels) even when model has no data; if I use ==null it does not show anything even if the model has data
 
HazIds is an IList model containing int and strings
 
 

Answers (3)