Search code examples
twitter-bootstrapvisual-studio-2015asp.net-corebootstrap-tabs

Make Bootstrap current tab active after a POST action in ASP.NET Core


In the following ASP.NET MVC Core View two different forms are used in each Bootstrap tab-pane. How can we make the tab in which the form was submitted active? Note: In real scenario there are more than two tabs. For brevity of this post I've included only two tabs.

<ul class="nav nav-tabs">
  <li class="active"><a data-toggle="tab" href="#home">Home</a></li>
  <li><a data-toggle="tab" href="#Tab1">Test Tab</a></li>
</ul>

<div class="tab-content">
  <div id="home" class="tab-pane in active">
    <form asp-controller="myController" asp-action="myAction1" method="post">
      <h3>HOME</h3>
      <p>Some content.</p>
      <input type="submit" name="GO" value="Value1" />
    </form>
  </div>
  <div id="Tab1" class="tab-pane">
    <form asp-controller="myController" asp-action="myAction2" method="post">
      <h3>Test</h3>
      <p>Some content in here.</p>
      <input type="submit" name="GO" value="Value2" />
    </form>
  </div>
</div>

Solution

  • You can add a class based on view model like this:

    <div class="tab-pane @(Model.HomeTab ? " active" : "")">
        ...
    </div>