skip to Main Content

I am Using MVC,Bootstrap in my project .I have Submit button to save data .my design consist of bootstrap nav tabs.
Now my problem is when I save data then I want to change my tab.
My flow is as below

  1. I enter data in first tab then click submit button.
    2.Ajax call saves data.after ajax call I wrote tab change tag $(‘.nav-tabs a[href="#Dependent"]’).tab(‘show’); It changes tab successfully.
    3.But after completion debugger again controller is executed and ActionResult index gets executed which then returns index view and then page is reloaded. and my tab is again changed to default first tab
    So my problem is how to how to keep changed tab after saving and page reload after save state.
    below is my index.chtml only tabs

  2. My Information
  3. @*data-toggle=”tab”*@

  4. Dependent Information
  5. Finalize
  6.      <div class="tab-content well">
             <div class="tab-pane active " id="info">
                 <form data-toggle="validate" role="form" id="defaultForm" method="post">
    
    
                     <table class="table">
                         <tbody>
    
                             <tr>
                                 <td class="auto-style4"></td>
                                 <td style="text-align: center" class="auto-style3"></td>
                                 <td class="auto-style6">&nbsp;</td>
                                 <td class="auto-style7">&nbsp;</td>
                                 <td>&nbsp;</td>
                                 <td>&nbsp;</td>
    
                             </tr>
                             <tr>
                                 <td></td>
                                 <td class="text-center">
                                     <label for="FirstName"> First Name</label>
                                 </td>
                                 <td class="text-center">
                                     <label for="MiddleName">Middle Name</label>
                                 </td>
    
                                 <td class="text-center">
                                     <label for="LastName">Last Name</label>
                                 </td>
                                 <td></td>
                                 <td>&nbsp;</td>
    
                             </tr>
                             <tr>
                                 <td class="text-center">
                                     <label for="Name">Name</label>
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="FirstName" placeholder="FirstName" required="required" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="MiddleName" placeholder="MiddleName" />
                                 </td>
    
                                 <td>
                                     <input type="text" class="form-control" id="LastName" placeholder="LastName" required="required" />
                                 </td>
                                 <td>&nbsp;</td>
                                 <td>&nbsp;</td>
    
                             </tr>
                             <tr>
                                 <td class="text-center">
                                     <label for="DOB">Date Of Birth</label>
                                 </td>
                                 <td>
    
                                     <input type="text" class="form-control" id="DOB" placeholder="MM/DD/YY" required="required" />
    
                                 </td>
                                 <td>
                                     <label for="Age">Age</label> <input type="text" class="form-control" id="MyAge" placeholder="Age" disabled="disabled" />
                                 </td>
    
                             </tr>
    
                             <tr>
                                 <td class="text-center">
                                     <label for="Department">Department</label>
                                 </td>
                                 <td>
    
                                     <div class="dropdown">
                                         <select id="ddDept" class="btn dropdown-toggle"><option></option></select>
                                     </div>
    
                                     @* <input type="text" class="dropdown" id="Dept" placeholder="Select" />*@
    
                                 </td>
    
                             </tr>
    
                             <tr>
                                 <td class="text-center">
                                     <label for="DOJ">Date Of Joining</label>
                                 </td>
                                 <td>
    
                                     <input type="text" class="form-control" id="DOJ" placeholder="MM/DD/YY" required="required" />
    
                                 </td>
                             </tr>
                             <tr>
                                 <td class="text-center">
                                     <label for="GrossSalary">Gross Salary</label>
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="GrossSalary" placeholder="GrossSalary" required="required" />
                                 </td>
                             </tr>
                             <tr>
                                 <td class="text-center">
                                     <label for="Tax">Tax</label>
                                 </td>
                                 <td>
    
                                     <div class="dropdown">
                                         <select id="ddTax" class="btn dropdown-toggle" onchange="calculateNetsal()"><option></option></select>
                                     </div>
    
                                     @* <input type="text" class="dropdown" id="Dept" placeholder="Select" />*@
    
                                 </td>
                             </tr>
                             <tr>
                                 <td class="text-center">
                                     <label for="NetSalary">Net Salary</label>
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="NetSalary" placeholder="NetSalary" disabled="disabled" />
                                 </td>
                             </tr>
                             <tr>
                                 <td></td>
                                 <td></td>
                                 <td>
                                     <button type="submit" class="btn btn-sm btn-primary" value="Create" id="btnAdd" onclick="TabChange(); ">Save and Next</button>
                                     <button type="submit" class="btn btn-sm btn-primary" id="btnClear" onclick="clear();" >Clear</button>
                                 </td>
                                 <td></td>
                                 <td></td>
    
                             </tr>
                         </tbody>
                     </table>
                 </form>
             </div>
    
             @*DEPENDENT*@
    
             <div class="tab-pane  " id="Dependent">
                 <form id="Dependent" method="post" action="">
                     <table class="table">
                         <tbody>
    
                             <tr>
                                 <td class="auto-style4">&nbsp;</td>
                                 <td style="text-align: center" class="auto-style3"></td>
                                 <td class="auto-style6">&nbsp;</td>
                                 <td class="auto-style7">&nbsp;</td>
                                 <td>&nbsp;</td>
                                 <td>&nbsp;</td>
    
                             </tr>
                             <tr>
                                 <td></td>
                                 <td class="text-center">
                                     <label for="Name">Name</label>
                                 </td>
                                 <td class="text-center">
                                     <label for="SDOB">Date Of Birth</label>
                                 </td>
    
                                 <td class="text-center">
                                     <label for="Age">Age</label>
                                 </td>
                                 <td></td>
                                 <td>&nbsp;</td>
    
                             </tr>
                             <tr>
                                 <td class="text-center">
                                     <label for="Spouse">Spouse</label>
                                 </td>
                                 <td>
    
                                     <input type="text" class="form-control" id="Name" placeholder="Name" />
    
    
                                 </td>
                                 <td>
    
                                     @* <input id="sdob" type="text" />*@
                                     <input type="text" class="form-control" id="S1DOB" placeholder="MM/DD/YY" />
    
                                 </td>
    
                                 <td>
    
                                     <input type="text" class="form-control" id="SAge" placeholder="Age" disabled="disabled" />
                                 </td>
    
                             </tr>
    
                             <tr>
                                 <td></td>
                                 <td class="text-center">
                                     <label for="CName">Name</label>
                                 </td>
                                 <td class="text-center">
                                     <label for="CDOB">Date Of Birth</label>
                                 </td>
                                 <td class="text-center">
                                     <label for="Age">Age</label>
                                 </td>
                                 <td>
    
                                     <label for="Relation">Relation</label>
    
                                 </td>
    
    
                             </tr>
    
                             <tr>
                                 <td class="text-center">
                                     <div class="custom-control custom-checkbox">
                                         <label class="custom-control-label" for="Chck1">Child 1</label>
                                         <input type="checkbox" class="custom-control-input" id="Chck1" value="1" onchange="validationCheck(Chck1); enableTextBox();" onclick=" PopulateDropDownList()" unchecked>
                                     </div>
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="CName" placeholder="Name" disabled="disabled" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="C1DOB" placeholder="MM/DD/YY" disabled="disabled" />
                                 </td>
    
                                 <td>
                                     <input type="text" class="form-control" id="C1Age" placeholder="Age" disabled="disabled" />
                                 </td>
                                 <td>
    
                                     <select id="ddlRelation" class="btn dropdown-toggle" disabled><option></option></select>
    
    
                                 </td>
                             </tr>
    
    
                             <tr>
                                 <td class="text-center">
                                     <div class="custom-control custom-checkbox">
                                         <label class="custom-control-label" for="Chck2">Child 2</label>
                                         <input type="checkbox" class="custom-control-input" id="Chck2" onchange="validationCheck(Chck2); enableTextBox();" value="2" onclick=" PopulateDropDownList2()" unchecked>
    
                                     </div>
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="C2Name" placeholder="Name" disabled="disabled" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="C2DOB" placeholder="MM/DD/YY" disabled="disabled" />
                                 </td>
    
                                 <td>
                                     <input type="text" class="form-control" id="C2Age" placeholder="Age" disabled="disabled" />
                                 </td>
                                 <td>
                                     <select id="ddlRelation2" class="btn dropdown-toggle" disabled><option></option></select>
                                     @* <input type="text" class="dropdown" id="Dept" placeholder="Select" />*@
    
                                 </td>
                             </tr>
                             <tr>
                                 <td class="text-center">
                                     <div class="custom-control custom-checkbox">
                                         <label class="custom-control-label" for="Chck3">Child 3</label>
                                         <input type="checkbox" class="custom-control-input" id="Chck3" onchange="validationCheck(Chck3); enableTextBox();" value="3" onclick=" PopulateDropDownList3()" unchecked>
    
                                     </div>
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="C3Name" placeholder="Name" disabled="disabled" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="C3DOB" placeholder="MM/DD/YY" disabled="disabled" />
                                 </td>
    
                                 <td>
                                     <input type="text" class="form-control" id="C3Age" placeholder="Age" disabled="disabled" />
                                 </td>
                                 <td>
                                     <select id="ddlRelation3" class="btn dropdown-toggle" disabled><option></option></select>
                                     @* <input type="text" class="dropdown" id="Dept" placeholder="Select" />*@
    
                                 </td>
                             </tr>
                             <tr>
                                 <td class="text-center">
                                     <div class="custom-control custom-checkbox">
                                         <label class="custom-control-label" for="Chck4">Child 4</label>
                                         <input type="checkbox" class="custom-control-input" id="Chck4" onchange="validationCheck(Chck4); enableTextBox();" value="4" onclick=" PopulateDropDownList4()" unchecked>
    
                                     </div>
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="C4Name" placeholder="Name" disabled="disabled" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="C4DOB" placeholder="MM/DD/YY" disabled="disabled" />
                                 </td>
    
                                 <td>
                                     <input type="text" class="form-control" id="C4Age" placeholder="Age" disabled="disabled" />
                                 </td>
                                 <td>
                                     <select id="ddlRelation4" class="btn dropdown-toggle" disabled><option></option></select>
                                     @* <input type="text" class="dropdown" id="Dept" placeholder="Select" />*@
    
                                 </td>
                             </tr>
    
    
                             @* PARENT*@
    
    
                             <tr>
                                 <td></td>
                                 <td class="text-center">
                                     <div class="custom-control custom-checkbox">
    
                                         <input type="checkbox" class="custom-control-input" id="chkFather" value="Father" OnChange="validationCheck1(chkFather); enableTextBox();" unchecked>
                                         <label class="custom-control-label" for="chkFather">Father</label>
    
                                     </div>
    
                                 </td>
                                 <td class="text-center">
                                     <div class="custom-control custom-checkbox">
    
                                         <input type="checkbox" class="custom-control-input" id="chkMother" value="Mother" OnChange="validationCheck1(chkMother); enableTextBox();" unchecked>
                                         <label class="custom-control-label" for="chkMother">Mother</label>
    
                                     </div>
    
    
                                 </td>
                                 <td class="text-center">
                                     <div class="custom-control custom-checkbox">
    
                                         <input type="checkbox" class="custom-control-input" id="chkfatherinlaw" value="FatherInLaw" OnChange="validationCheck1(chkfatherinlaw); enableTextBox();" unchecked>
                                         <label class="custom-control-label" for="chkfatherinlaw">Father In Law</label>
    
                                     </div>
    
                                 </td>
                                 <td class="text-center">
                                     <div class="custom-control custom-checkbox">
    
                                         <input type="checkbox" class="custom-control-input" id="chkmotherInLaw" value="MotherInLaw" OnChange="validationCheck1(chkmotherInLaw); enableTextBox();" unchecked>
                                         <label class="custom-control-label" for="chkmotherInLaw">Mother In Law</label>
    
                                     </div>
    
    
    
                                 </td>
    
    
                             </tr>
    
                             <tr>
                                 <td class="text-center">
                                     <label for="Name">Name</label>
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="FName" placeholder="Name" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="MName" placeholder="Name" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="FInLName" placeholder="Name" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="MInLName" placeholder="Name" />
                                 </td>
    
                             </tr>
                             <tr>
                                 <td class="text-center">
                                     <label for="DOB">Date Of Birth</label>
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="FDOB" placeholder="MM/DD/YY" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="MDOB" placeholder="MM/DD/YY" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="FInLDOB" placeholder="MM/DD/YY" />
                                 </td>
                                 <td>
                                     <input type="text" class="form-control" id="MInLDOB" placeholder="MM/DD/YY" />
                                 </td>
    
                             </tr>
    
                             <tr>
                                 <td class="text-center"></td>
                                 <td>
                                     <div class="custom-control custom-checkbox">
    
                                         <input type="checkbox" class="custom-control-input" id="FDCh" onchange="loaddropdown(); enableTextBox();" unchecked>
                                         <label class="custom-control-label" for="FDCh">I Dont Know DOB</label>
    
                                     </div>
                                 </td>
                                 <td>
                                     <div class="custom-control custom-checkbox">
    
                                         <input type="checkbox" class="custom-control-input" id="MDCh" onchange="loaddropdown2();enableTextBox();" unchecked>
                                         <label class="custom-control-label" for="MDCh">I Dont Know DOB</label>
    
                                     </div>
                                 </td>
                                 <td>
                                     <div class="custom-control custom-checkbox">
    
                                         <input type="checkbox" class="custom-control-input" id="FInLDCh" onchange="loaddropdown3(); enableTextBox();" unchecked>
                                         <label class="custom-control-label" for="FInLDCh">I Dont Know DOB</label>
    
                                     </div>
                                 </td>
                                 <td>
                                     <div class="custom-control custom-checkbox">
    
                                         <input type="checkbox" class="custom-control-input" id="MInLDCh" onchange="loaddropdown4(); enableTextBox();" unchecked>
                                         <label class="custom-control-label" for="MInLDCh">I Dont Know DOB</label>
    
                                     </div>
                                 </td>
    
                             </tr>
    
                             <tr>
                                 <td class="text-center"></td>
                                 <td align="center">
                                     <div class="dropdown">
                                         <select id="ddFAge" class="btn dropdown-toggle"><option></option></select>
                                     </div>
                                     <label for="Age">Age</label>
                                     <input type="text" class="form-control" id="FAge" placeholder="Age" disabled="disabled" />
    
                                 </td>
                                 <td align="center">
                                     <div class="dropdown">
                                         <select id="ddMAge" class="btn dropdown-toggle"><option></option></select>
                                     </div>
    
                                     <label for="Age">Age</label>
                                     <input type="text" class="form-control" id="MAge" placeholder="Age" disabled="disabled" />
    
                                 </td>
                                 <td align="center">
                                     <div class="dropdown">
                                         <select id="ddFInLAge" class="btn dropdown-toggle"><option></option></select>
                                     </div>
                                     <label for="Age">Age</label>
                                     <input type="text" class="form-control" id="FInLAge" placeholder="Age" disabled="disabled" />
    
                                 </td>
                                 <td align="center">
                                     <div class="dropdown">
                                         <select id="ddMInLAge" class="btn dropdown-toggle"><option></option></select>
                                     </div>
    
                                     <label for="Age">Age</label>
                                     <input type="text" class="form-control" id="MInLAge" placeholder="Age" disabled="disabled" />
    
                                 </td>
    
                             </tr>
                             <tr>
                                 <td></td>
                                 <td></td>
                                 <td>
                                     <button type="submit" class="btn btn-sm btn-primary" id="btnAdd1" onclick="TabChange();">Save and Next</button>
                                 </td>
                                 <td>
                                     <button type="submit" class="btn btn-primary" id="btnClear" onclick="clear1();">Clear</button>
                                 </td>
                                 <td></td>
    
                             </tr>
                         </tbody>
    
                     </table>
                 </form>
             </div>
         </div>
    
     </div>
    
    <script>
                $(document).ready(function() {
                   
                    $("#btnAdd").click(function () {
                        debugger;
                        $.ajax(
                        {
                            type: "POST", 
                            url: "/Home/Create11",   
                            data: {FirstName:$("#FirstName").val(),                                   MiddleName:$("#MiddleName").val(),
                                LastName: $("#LastName").val(),
                                DOB: $("#DOB").val(),
                                Age: $("#MyAge").val(),
                                Department: $("#ddDept").val(),  
                                DOJ: $("#DOJ").val(),
                              GrossSal:$("#GrossSalary").val(),
                               Tax: $("#ddTax   option:selected").text(),  
                                NetSal: $("#NetSalary").val()
                            },
                            async:true,
                            success: function (data) {
                           }
                        });
                        $('.nav-tabs a[href="#Dependent"]').tab('show');
                    });
                });

    </script>

2

Answers


  1. In the controller Index method create a optional parameter for the selected tab. Default to tab 0 but when you call from the javascript code set the tab to the one you want.

    Login or Signup to reply.
  2. Handle the onsubmit event into the form tag and prevent that:

     <form data-toggle="validate" role="form" id="defaultForm" method="post" onsubmit="event.preventDefault();">
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search