Reputation: 51
I'm building an MVC application and in my view i use bootstrap tabs. Now, what i want to to is to reload last active tab after refreshing the page or redirecting to it from the controller. Any help on how can i do this in the simplest way? This is my code:
<div id="tabs">
<ul class="nav nav-tabs">
<li class="active">
<a href="#Stages" data-toggle="tab">Stages</a>
</li>
<li>
<a href="#Activities" data-toggle="tab">Activities</a>
</li>
<li>
<a href="#History" data-toggle="tab">History</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="Stages">
@Html.Partial("_Stages", Model.stages)
</div>
<div class="tab-pane" id="Activities">
@Html.Partial("_Activities", Model.activities)
</div>
<div class="tab-pane" id="History">
@Html.Partial("_History", Model.history)
</div>
</div>
</div>
Upvotes: 2
Views: 2233
Reputation: 11203
TwitterBootstrapMVC allows passing active tab number through an extension method:
using (var tabs = Html.Bootstrap().Begin(new Tabs("TabsId").ActiveTab(2)))
{
@tabs.Tab("Tab 1")
@tabs.Tab("Tab 2")
@tabs.Tab("Tab 3")
using (tabs.BeginPanel())
{
<p>Contents of tab 1</p>
}
using (tabs.BeginPanel())
{
<p>Contents of tab 2</p>
}
using (tabs.BeginPanel())
{
<p>Contents of tab 3</p>
}
}
So if you have information in your model about the tab you want to be on, you could use that.
Upvotes: 0
Reputation: 91
Try checking location.href.hash
on client side and manipulate tab selection accordingly.
Or else you will have to use some navigation/history client libraries like nav.js
or sammy.js
. This can register tab clicks in browser history stack with hash path. And with URL hash you can manipulate the tab selection
You can refer to Sammy Implementation
Upvotes: 1