0

Question: In a Bootstrap Tabs panel, how can we display an MVC View page in the corresponding tab-content area?

Details: Following example (taken from here) works fine in my ASP.NET MVC Core project. But, using anchor tag helper I want to call an action method when user clicks on a tab; when I do that I get the following error. For example, I get this error when in tab Menu 1 I use asp-controller and asp-action attributes as follows. I know the error is related to href attribute that is related to the corresponding tab contents. Is there a workaround so we can still call an action method when a tab is clicked?:

Error:

Cannot override the 'href' attribute for <a>.

Error is on this line: <li><a data-toggle="tab" href="#menu1" asp-controller="myController asp-action="myAction">Menu 1</a></li>

Following works if anchor tag helper is not used:

<div class="container">
  <h2>Dynamic Tabs</h2>
  <ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#home">Home</a></li>
    <li><a data-toggle="tab" href="#menu1">Menu 1</a></li>
    <li><a data-toggle="tab" href="#menu2">Menu 2</a></li>
    <li><a data-toggle="tab" href="#menu3">Menu 3</a></li>
  </ul>

  <div class="tab-content">
    <div id="home" class="tab-pane fade in active">
      <h3>HOME</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    </div>
    <div id="menu1" class="tab-pane fade">
      <h3>Menu 1</h3>
      <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </div>
    <div id="menu2" class="tab-pane fade">
      <h3>Menu 2</h3>
      <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.</p>
    </div>
    <div id="menu3" class="tab-pane fade">
      <h3>Menu 3</h3>
      <p>Eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
    </div>
  </div>
</div>

1 Answer 1

0

What you need to do is to call @Html.RenderAction() inside the required tab content. Leave the href as they are, there purpose is only to show the corresponding tab based on tab id.

See the following Question: Create controller for partial view in ASP.NET MVC

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.