I am using bootstrap 3 tabs in my mvc view. I want to render another partial view on tab change. Here is the code for the tab
<ul class="nav nav-tabs">
<li class="active" id="studentList">
<a href="#tab_1_1" data-toggle="tab" aria-expanded="true">
Student List </a>
</li>
<li class="" id="studentAddEdit">
<a href="#tab_1_2" data-toggle="tab" aria-expanded="false">
Student Add/Edit </a>
</li>
<div class="tab-content">
<div class="tab-pane fade active in" id="tab_1_1">
<p>
@Html.Action("StudentList","Student")
</p>
</div>
<div class="tab-pane fade" id="tab_1_2">
<p>
@Html.Action("StudentAddEdit","Student", new {id=Model.StudentId})
</p>
</div>
It renders the studentAddEdit
view on view load. I want to render studentAddEdit
view again when the user changes the tab and selects the studentAddEdit
tab. Any solution suggested? I am currently doing it with jquery but not succeeding.
To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.
A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.
RenderPartial function to render Partial View in ASP.Net MVC Razor. The data will be fetched from database using Entity Framework and then the Partial View will be rendered using the @Html. RenderPartial function in ASP.Net MVC Razor.
First of all slight changes in your html like adding a class
to your anchor tags and adding extra data-*
attribute to the same and the you can render partial view through jquery as below:
<ul class="nav nav-tabs">
<li class="active" id="studentList">
<a href="#tab_1_1" class="tbs" data-info="stdlist" data-toggle="tab" aria-expanded="true">
Student List </a>
</li>
<li class="" id="studentAddEdit">
<a href="#tab_1_2" data-toggle="tab" class="tbs" data-info="stdaddedit" aria-expanded="false">
Student Add/Edit </a>
</li>
</ul>
Your JS would be something like below:
$('.tbs').on('click',function(){
var info=$(this).data('info');
switch(info)
{
case 'stdlist':$('#tab_1_1 p').load('/Student/StudentList'); //Controller method which returns partial view
break;
case 'stdaddedit':$('#tab_1_2 p').load('/Student/StudentAddEdit');//Controller method which returns partial view
break;
//Add cases if you want
default:break;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With