简体   繁体   中英

Redirect to specific tab from another page in asp.net

Good day all, I'm using Bootstrap 3 in asp.net mvc 4 & I'm trying to make tabs with bootstrap. I want other pages to redirect to a specific tab when clicked. I'm new to asp.net so I can't figure it out how can I do that. Here is my code,

Page that has the tabs(Service_BO.cshtml)

<div class="container well" style="min-width: 100%; padding-right: 5px;">
    <h3>BO Account Opening</h3><hr style="border-top: 2px solid #096596;" />
    <ul class="nav nav-tabs">
        <li><a href="#bo" role="tab" data-toggle="tab">BO Account Opening</a></li>
        <li><a href="#ipo" role="tab" data-toggle="tab">IPO Application</a></li>
        <li><a href="#smi" role="tab" data-toggle="tab">Share Market Investment</a></li>
    </ul>
    <div class="tab-content">
        <div class="tab-pane active" id="bo">
            <p>BO</p>
        </div>
        <div class="tab-pane" id="ipo">
            <p>IPO</p>
        </div>
        <div class="tab-pane" id="smi">
            <p>SMI</p>
        </div>
    </div>
</div>

External Page(Services.cshtml>

<div class="col-sm-6 col-md-3">
    <img class="img-rounded img-responsive" src="~/Images/modhumita01.jpg" />
    <a href="@Url.Action("Service_BO", "Home")><h3>BO Account Opening</h3></a>
    <span><b>Open an individual and/or joint account</b></span>
</div>

Is there any way I can redirect to a specific tab by clicking a link from other page? It'll be a life saving if anyone can help me. Tnx.

You can resolve the active tab in your controller and pass the value to view (in your ViewModel, or ViewBag). Then you can use jquery to set the active class:

Controller

//empty value or a default
var activeTab = "bo";

//selecting active tab logic here
//...
ViewBag.Active = activeTab;

Service_BO.cshtml

@{
    //value is 'bo', 'ipo' or 'smi'
    string active = ViewBag.Active.ToString(); 
}

<script>
$(function () {
    var selector = '@active';
    $("#" + selector).addClass("active");
});
</script>

Can do something like this based on link shown above in comments

/* page load */
$(function(){
  var hash = window.location.hash;
  hash && $('ul.nav a[href="' + hash + '"]').tab('show');  


  /* add hash to url when tabs selected (for bookmarking) */
  $('.nav-tabs a').on('shown', function (e) {
      window.location.hash = this.hash;
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM