culdaffe
culdaffe

Reputation: 39

Is there a way to put an if statement within a foreach loop in a cshtml page?

I am studying mvc and user interface. I am trying to display and check user details and if a certain variable is true, I want a bootstrap toggle on icon to appear, and if false, I want it to be a toggle off icon. So if s.Active is true, instead of true, I want the on icon. Can anyone advise on this?

@foreach(var s in Model.Tickets) {
           <tr>
               <td>@s.Issue</td>
               <td>@s.CreatedOn</td>
               <td>@s.Active</td>
               
           </tr>
       }```

Upvotes: 2

Views: 957

Answers (1)

husam i
husam i

Reputation: 74

ok, you can use if with for

@foreach(var s in Model.Tickets) {
           <tr>
               <td>@s.Issue</td>
               <td>@s.CreatedOn</td>
               <td>
               @if (s.Active)
                   {
                    <div class="custom-control custom-switch">
                    <input type="checkbox" class="custom-control-input" id="customSwitch1" checked>
                    <label class="custom-control-label" for="customSwitch1"></label>
                    </div>
                   }
                else
                   {
                     <div class="custom-control custom-switch">
                    <input type="checkbox" class="custom-control-input" id="customSwitch1">
                    <label class="custom-control-label" for="customSwitch1"></label>
                     </div>
                    }
               </td>
           </tr>
       }

this toggle work according to bootstrap, i used this https://bootswatch.com/litera/

or you also could use your own toggle style

Upvotes: 1

Related Questions