作爲標題狀態。添加css類到一個MVC內部的jQuery的foreach循環
我想在我的模型中添加基於特定值的標題。
目前看起來像這樣。
foreach (var track in Model)
{
if (track.G1)
{
<script type="text/javascript">
jQuery(".model-attribute-wrapper").addClass("G1");
</script>
}
else if (track.Handicap)
{
<script type="text/javascript">
jQuery(".model-attribute-wrapper").addClass("handicap");
</script>
}
else if (track.Special)
{
<script type="text/javascript">
jQuery(".model-attribute-wrapper").addClass("special");
</script>
}
else if (track.BestRight)
{
<script type="text/javascript">
jQuery(".model-attribute-wrapper").addClass("bestright");
</script>
}
else if (track.Dirt)
{
<script type="text/javascript">
jQuery(".model-attribute-wrapper").addClass("dirt");
</script>
}
if (even)
{
even = false;
<div class="model-attribute-wrapper even">
<div class="model-attribute xx-small">@Html.DisplayFor(modelItem => track.Id) </div>
<div class="model-attribute huge">@Html.DisplayFor(modelItem => track.Name) </div>
<div class="model-attribute small">@Html.DisplayFor(modelItem => track.Handicap) </div>
<div class="model-attribute small">@Html.DisplayFor(modelItem => track.Special) </div>
<div class="model-attribute small">@Html.DisplayFor(modelItem => track.Dirt) </div>
<div class="model-attribute medium">@Html.DisplayFor(modelItem => track.BestRight) </div>
<div class="model-attribute small">@Html.DisplayFor(modelItem => track.G1) </div>
<div class="model-attribute small">@Html.DisplayFor(modelItem => track.Distance) </div>
<div class="model-attribute action">
<div class="model-action">@Html.ActionLink("Edit", "Edit", new { id = track.Id }) </div>
<div class="model-action">@Html.ActionLink("Delete", "Delete", new { id = track.Id }) </div>
</div>
</div>
}
else
{
even = true;
<div class="model-attribute-wrapper odd">
<div class="model-attribute xx-small">@Html.DisplayFor(modelItem => track.Id) </div>
<div class="model-attribute huge">@Html.DisplayFor(modelItem => track.Name) </div>
<div class="model-attribute small">@Html.DisplayFor(modelItem => track.Handicap) </div>
<div class="model-attribute small">@Html.DisplayFor(modelItem => track.Special) </div>
<div class="model-attribute small">@Html.DisplayFor(modelItem => track.Dirt) </div>
<div class="model-attribute medium">@Html.DisplayFor(modelItem => track.BestRight) </div>
<div class="model-attribute small">@Html.DisplayFor(modelItem => track.G1) </div>
<div class="model-attribute small">@Html.DisplayFor(modelItem => track.Distance) </div>
<div class="model-attribute action">
<div class="model-action">@Html.ActionLink("Edit", "Edit", new { id = track.Id }) </div>
<div class="model-action">@Html.ActionLink("Delete", "Delete", new { id = track.Id }) </div>
</div>
</div>
}
}
這似乎不工作,因爲它增加了大量的類的前2行,然後它似乎不會增加一個。
其實我fixxed不同的看法。沒有涉及JavaScript。 'string style =「」; if(track.G1){style =「G1」; } else if(track.Handicap){style =「handicap」; } else if(track.Special){style =「special」; } else if(track.BestRight){style =「bestright」; } else if(track.Dirt){style =「dirt」; } else if(even){style =「even」; even = false;} else if(!even){style =「odd」; even = true;}' 然後我在我的div類 – Puzzle84
中渲染這個字符串,這是好得多。 – Birey