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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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>
}