2014-01-07 5 views
0

У меня есть следующий код в моей веб-странице:Динамически BackgroundColor изменения на основе значения в модели

@model IEnumerable<LiveTile.Models.MenuItem> 

@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml"; 
} 

@foreach (var item in Model) { 
    <div class="tile tile-singlewidth"> 
     <div class="tilecontent tile-singlewidth"> 
      @Html.DisplayFor(modelItem => item.Name)<p> 
      <img src="@Url.Content(item.Url)" height="30" alt="Image" title="@Html.DisplayFor(modelItem => item.ActionName)" /> 
     </div> 
    </div> 
} 

В моей CSS У меня есть плитка ID и плитка-идентификатор ошибка.

.tile 
{ 
    float: left; 
    margin-right: 10px; 
    margin-bottom: 10px; 
    height: 75px; 
    background-color: #9DB029; 
    font-family: "Segoe UI" ,Helvetica,Garuda,Arial,sans-serif; 
} 

.tile-error 
{ 
    float: left; 
    margin-right: 10px; 
    margin-bottom: 10px; 
    height: 75px; 
    background-color: #f00; 
    font-family: "Segoe UI" ,Helvetica,Garuda,Arial,sans-serif; 
} 

Теперь на основе значения в модели, я хочу, чтобы изменить к

Может кто-нибудь мне помочь?

ответ

0

Я нашел следующее решение:

@foreach (var item in Model) { 

    string cssClass = "tile tile-singlewidth"; 

    if (item.ControllerName == "red") 
    { 
     cssClass = "tile-error tile-singlewidth"; 
    } 

    <div class="@cssClass"> 
     <div class="tilecontent tile-singlewidth"> 
      @Html.DisplayFor(modelItem => item.Name)<p> 
      <img src="@Url.Content(item.Url)" height="30" alt="Image" title="@Html.DisplayFor(modelItem => item.ActionName)" /> 
     </div> 
    </div> 
} 
Смежные вопросы