嘗試這樣的:
<div class="tab-pane @(Model.ActiveTab == 1 ? " active" : "")" id="1">
...
</div>
或者爲了避免這種情況rible標籤湯編寫自定義的HTML幫助:
public static class TabExtensions
{
private class TabControl: IDisposable
{
private readonly ViewContext _viewContext;
public TabControl(ViewContext viewContext)
{
_viewContext = viewContext;
}
public void Dispose()
{
_viewContext.Writer.Write("</div>");
}
}
public static IDisposable Tab(this HtmlHelper htmlHelper, bool active, object htmlAttributes)
{
var div = new TagBuilder("div");
div.MergeAttributes(new RouteValueDictionary(htmlAttributes));
div.AddCssClass("tab-pane");
if (active)
{
div.AddCssClass("active");
}
htmlHelper.ViewContext.Writer.Write(div.ToString(TagRenderMode.StartTag));
return new TabControl(htmlHelper.ViewContext);
}
}
可能這樣使用:
@using (Html.Tab(Model.ActiveTab == 1, new { id = "tab1" }))
{
...
}
,並在這種情況下會發出:
<div class="tab-pane active" id="tab1">
...
</div>
IS作爲ActiveTab的價值預期? – 2012-04-19 13:41:53