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

    Change row color depend on condition in razor syntax

    I have one page where I written below code.

    @foreach (var item in Model)
    @Html.DisplayFor(modelItem => item.NotificationType)
    @Html.DisplayFor(modelItem => item.NextActionTaker)

    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

    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">

  • #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 {
    .approved {
    .rejected {
    .pending {

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

    @foreach (var rows2 in rows1)

    <tr class="@rows2.requestStatus">



  • Sign In to post your comments