1

Please explain how can I change the color of the text in the row(i think it should be class for ) depending on the value of the element item.Status.

<tbody>
  @foreach (var item in Model) {
  <tr>
    <td>
      @{ if (item.Status != 0) {
      <del> @Html.DisplayFor(modelItem => item.Name)</del> } else { @Html.DisplayFor(modelItem => item.Name) } }
    </td>
    <td>
      @Html.DisplayFor(modelItem => item.Count)
    </td>
  </tr>
Tiranu
  • 35
  • 6
  • What `string` are you referring to? – mxmissile Mar 19 '19 at 13:58
  • Possible duplicate of [Applying css class using Html.DisplayFor inside razor view](https://stackoverflow.com/questions/33546963/applying-css-class-using-html-displayfor-inside-razor-view) – Yarl Mar 19 '19 at 14:01
  • See also https://stackoverflow.com/questions/6365633/what-is-the-html-displayfor-syntax-for. – Yarl Mar 19 '19 at 14:03

1 Answers1

0

You can use auxiliary variable for this.

@foreach (var item in Model)
{
  bool itemStatusOk = item != 0;
  <tr class="@(itemStatusOk ? "X" : "Y")">
    <td>
      @if (itemStatusOk)
        { <del> @Html.DisplayFor(modelItem => item.Name)</del> }
        else
        { @Html.DisplayFor(modelItem => item.Name) }
    </td>
    <td>
      @Html.DisplayFor(modelItem => item.Count)
    </td>
  </tr>
 }
Yarl
  • 728
  • 1
  • 7
  • 26