asp.net-mvchtmlrazorrazor-2

How to put a row in red with Razor in MVC5


I have a table, which I want to mark when the real stock is less than the minimum stock, for that I'm going to render the view using Razor, but I'm not getting results

I enclose my view that contains the table that we wish to intervene

        <table class="table table-bordered table-hover table-condensed">
            <tr>

                <th>
                    @Html.DisplayNameFor(model => model.First().v_Nombre)
                </th>

                <th>
                    @Html.DisplayNameFor(model => model.First().StockReal)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.First().StockMinimo)
                </th>
                <th></th>
            </tr>

            @foreach (var item in Model)
            {
                var fila = string.Empty;

                if(item.StockReal < item.StockMinimo)
                {
                    fila = "danger";
                }

                <tr class="@fila">
                    <td>                              
                        @Html.DisplayFor(modelItem => item.v_Nombre)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.StockReal)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.StockMinimo)
                    </td>
                    <td>
                        <a href="#" class="btn btn-outline-warning" onclick="EditarProducto(@item.Kn_CodigoProducto)">Editar </a>
                    </td>
                </tr>
            }

        </table>

Expected behavior: that the row where the real stock is less than the minimum stock turns red

Behavior Achieved: No change in my view

what am I doing wrong? What is missing in my Razor code? any help for me?


Solution

  • In bootstrap 4 the class is table-danger so change your code:

    if(item.StockReal < item.StockMinimo)
    {
        fila = "table-danger";
    }