DotnetSparrow
DotnetSparrow

Reputation: 27996

div color in razor view

I am displaying a list of products in my asp.net MVC 3 razor view. I am displaying products in foreach loop inside div. I want that if product is on sale then div color is red. How can I change background color of div in razor view?

Upvotes: 4

Views: 4068

Answers (1)

Codo
Codo

Reputation: 78795

Razor view:

@foreach (Product product in Model.ProductList)
{
    string saleClass = product.IsOnSale ? "onSale" : "regular";
    <div class="@saleClass">
        @product.Description
    </div>
}

Stylesheet:

onSale
{
    background-color: red;
}

regular
{
    background-color: white;
}

Or in a more direct fashion:

@foreach (Product product in Model.ProductList)
{
    string style= product.IsOnSale ? "background-color: red" : "background-color: white";
    <div style="@style">
        @product.Description
    </div>
}

Upvotes: 4

Related Questions