You must Sign In to post a response.
  • Category: ASP.Net MVC

    Change row color depend on condition in razor syntax

    Hi.
    I have one page where I written below code.

    @foreach (var item in Model)
    {
    <tr>
    <td>
    @Html.DisplayFor(modelItem => item.NotificationType)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.NextActionTaker)
    </td>
    </tr>
    }

    Here, I want to change row color by adding condition while creating <tr>.
    I have written if else statement which was not working properly.
    Please suggest.
  • #768255
    Hi,

    Use @ operator.
    Razor requires tags to be well-formed. So, you can force Razor to ignore the tag by prefixing them with operator @

    string style = Item != Item1 ? "background-color:Yellow" : "background-color:Blue" ;
    <tr style="@style">
    ...
    </tr>

  • #768260
    You can use following code snippet to change the background color of the table row based on the requeststatus

    <style type="text/css">
    .grey {
    background-color:grey;
    }
    .approved {
    background-color:green;
    }
    .rejected {
    background-color:red;
    }
    .pending {
    background-color:lime;
    }
    </style>

    <fieldset>
    <legend>Your Requests</legend>
    <table border="1" width="100%">
    <tr class="grey">
    <th>Description</th>
    <
    </tr>

    @foreach (var rows2 in rows1)
    {

    <tr class="@rows2.requestStatus">
    <td>@rows2.description</th>

    </tr>
    }
    </table>

    </fieldset>


Sign In to post your comments