1

I'm new in asp.net mvc and My problem is, I want to conditionally add a CSS background-color to a set of table rows, based on BILL-AMOUNT = 0.00 i have code like this

@model IEnumerable

View

<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.TicketNo)</td>
            <td style="padding-left: 12px; display: none;">@Html.DisplayFor(modelItem => item.name)</td>
            <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.ArrDate)</td>
            <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.quantity)</td>
            <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.Total)</td>
            <td>@Html.DisplayFor(modelItem => item.Refund)</td>
            <td>@Html.DisplayFor(modelItem => item.TotalAmount)</td>    
        </tr>
    }
</tbody>
3

3 Answers 3

1

Assuming you have a class:

.tr-zero {
    background-color: #cccccc;
}

You can add the class conditionally like this:

<tr@(item.TotalAmount == 0 ? " class=\"tr-zero\"" : string.Empty)>
Sign up to request clarification or add additional context in comments.

Comments

0
<tbody>
          @foreach (var item in Model)
            {
              <tr bgcolor='@(item.AMOUNT==0? "#FFF":"#EEE")'>
              <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.TicketNo)</td>
               <td style="padding-left: 12px; display: none;">@Html.DisplayFor(modelItem => item.name)</td>
               <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.ArrDate)</td>
               <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.quantity)</td>
               <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.Total)</td>
               <td>@Html.DisplayFor(modelItem => item.Refund)</td>
               <td>@Html.DisplayFor(modelItem => item.TotalAmount)</td>    
                    </tr>
                   }

Comments

0
        <tbody>
        @foreach (var item in Model)
        {
          <tr style="background-color:@(Convert.ToInt32(item.TotalAmount) == 0?"RedColor":"GreenColor")">
          <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.TicketNo)</td>
           <td style="padding-left: 12px; display: none;">@Html.DisplayFor(modelItem => item.name)</td>
           <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.ArrDate)</td>
           <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.quantity)</td>
           <td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.Total)</td>
           <td>@Html.DisplayFor(modelItem => item.Refund)</td>
           <td>@Html.DisplayFor(modelItem => item.TotalAmount)</td>    
          </tr>
         }
    </tbody>    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.