Bootstrap选项卡不适用于具有两个部分视图的视图

时间:2017-05-18 07:38:15

标签: asp.net-mvc twitter-bootstrap tabs asp.net-mvc-partialview

我有一个主视图,它有两个标签。第一个选项卡是默认选项卡,应显示第一个局部视图,第二个选项卡应显示第二个局部视图。虽然我正在使其正常工作,但我无法为特定的选定选项卡突出显示选项卡。

我的控制器。

public ActionResult Contact(int condition=0)
    {

        if(condition==0)
            ViewBag.Message = "Tab1";
        else
            ViewBag.Message = "Tab2";

        ViewBag.Condition = condition;
        return View();
    }

我的观点

@{
ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>


<div>
<ul class="nav nav-tabs">
    <li>@Html.ActionLink("Tab1","Contact", new { condition=0})</li>
    <li>@Html.ActionLink("Tab2", "Contact", new { condition = 1 })</li>
</ul>
<div class="tab-content">
    @if (ViewBag.Condition == 0)
    {
    <div class="tab-pane active" id="Tab1">
        @Html.Partial("_Partial1")
    </div>
    }
    else
    {
    <div class="tab-pane active" id="Tab2">
        @Html.Partial("_Partial2")
    </div>
    }
</div>

</div>

编辑1:正如答案所建议的那样,并没有解决问题。

@{
ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>


<div>
<ul class="nav nav-tabs">
    <li>@Html.ActionLink("Tab1","Contact", new { condition=0}, new { @class = ViewBag.Condition == 0 ? "active" : "" })</li>
    <li>@Html.ActionLink("Tab2", "Contact", new { condition = 1 }, new { @class = ViewBag.Condition == 1 ? "active" : "" })</li>
</ul>
<div class="tab-content">
    @if (ViewBag.Condition == 0)
    {
    <div class="tab-pane active" id="Tab1">
        @Html.Partial("_Partial1")
    </div>
    }
    else
    {
    <div class="tab-pane active" id="Tab2">
        @Html.Partial("_Partial2")
    </div>
    }
</div>

</div>

编辑2:

   <div id="tabs">
   <ul class="nav nav-tabs">

        <li class="nav active">@Html.ActionLink("Tab1", "Contact", new { condition = 0 }, new { @id = "Tab1", data_toggle = "tab" })</li>
        <li class="nav">@Html.ActionLink("Tab2", "Contact", new { condition = 1 }, new { @id = "Tab2", data_toggle = "tab" })</li>

</ul>
<div class="tab-content">

    <div class="tab-pane fade in active" id="Tab1">
        @Html.Partial("_Partial1")
    </div>

    <div class="tab-pane fade" id="Tab2">
        @Html.Partial("_Partial2")
    </div>

</div>

</div>

我已将代码更改为基于this fiddle 没有太大区别。

2 个答案:

答案 0 :(得分:2)

我确信有更好的方法可以做到,但这个解决方案对我有用。我每次只检查Viewbag并根据检查设置li的类。这样我就会设置正确的li元素,即使屏幕上有关于制表符更改的操作方法的调用。

<div>
<ul class="nav nav-tabs">
    @if (ViewBag.Condition == 0)
    {
        <li class="nav active">@Html.ActionLink("Tab1", "Contact", new { condition = 0 }, new { @id = "Tab1" })</li>
        <li class="nav">
            @Html.ActionLink("Tab2", "Contact", new { condition = 1 }, new{@id = "Tab2"})
        </li>
    }
    else if (ViewBag.Condition == 1)
    {
        <li class="nav">@Html.ActionLink("Tab1", "Contact", new { condition = 0 }, new { @id = "Tab1"})</li>
        <li class="nav active">@Html.ActionLink("Tab2", "Contact", new { condition = 1 }, new { @id = "Tab2"})</li>
    }
</ul>
<div class="tab-content">
    @if (ViewBag.Condition == 0)
    {
        <div class="tab-pane fade in active" id="Tab1">
            @Html.Partial("_Partial1")
        </div>
    }
    else if (ViewBag.Condition == 1)
    {
        <div class="tab-pane fade  in active" id="Tab2">
            @Html.Partial("_Partial2")
        </div>
    }
</div>

答案 1 :(得分:0)

<div>
    <ul class="nav nav-tabs">
        <li>@Html.ActionLink("Tab1", "Contactme", new { condition = 0 })</li>
        <li>@Html.ActionLink("Tab2", "Contactme", new { condition = 1 })</li>
    </ul>
    <div class="tab-content">
        @if (ViewBag.Condition == 0)
        {
            <div class="tab-pane active" id="Tab1">
               @Html.Partial("_Partial1")
            </div>
        }
        else
        {
            <div class="tab-pane active" id="Tab2">
                @Html.Partial("_Partial2")
            </div>
        }
    </div>
    </div>