Skip to content
Loading
How can I have a specific tab refreshed on post in Asp.Net Core Razor  
  •                 class="col-3">  
  •                     for="EquipmentUsed" class="form-control badge-primary">Equipment Model & ID  
  •                     for="GasTesting.EquipmentUsed" class="form-control" style="font-size:9px" />  
  •                   
  •                 class="col-1">  
  •                     for="Oxygen" class="form-control badge-primary">Oxygen  
  •                     for="GasTesting.Oxygen" class="form-control" style="font-size:9px" />  
  •                   
  •                 class="col-1">  
  •                     for="LEL" class="form-control badge-primary">LEL  
  •                     for="GasTesting.LEL" class="form-control" style="font-size:9px" />  
  •                   
  •                 class="col-1">  
  •                     for="Toxic" class="form-control badge-primary">Toxic Gas  
  •                     for="GasTesting.Toxic" class="form-control" style="font-size:9px" />  
  •                   
  •               
  •             "submit" class="btn btn-link btn-sm border-primary mb-2 mt-2" value="Save Data" />  
  •           
  •           
  •         class="tab-c">  
  •             "GasTest" />  
  •           
  •       
  •     "IsoCert" class="tab-pane fade">  
  •         

    Isolation Certificate

      
  •       
  • How do I fit this in the javascript? For each tab
     
    Second issue. My partial view looks like
     
    1. @page  
    2. @model Test.Test3Model  
    3.   
    4.   
    5. class="row no-gutters">  
    6.     class="col-3">  
    7.         for="DateGT" class="form-control badge-primary">Gas Testing Date  
    8.         class="form-control" style="font-size:12px">@Html.DisplayFor(model => model.GasTesting.DateGT)  
    9.     
      
  •     class="col-3">  
  •         for="NameGT" class="form-control badge-primary">Gas Tester  
  •         class="form-control" style="font-size:12px">@Html.DisplayFor(model => model.GasTesting.NameGT)  
  •     
  •   
  •     class="col-3">  
  •         for="EquipmentUsed" class="form-control badge-primary">Equipment Model & ID  
  •         class="form-control" style="font-size:12px">@Html.DisplayFor(model => model.GasTesting.EquipmentUsed)  
  •       
  •     class="col-1">  
  •         for="Oxygen" class="form-control badge-primary">Oxygen  
  •         class="form-control" style="font-size:12px">@Html.DisplayFor(model => model.GasTesting.Oxygen)  
  •       
  •     class="col-1">  
  •         for="LEL" class="form-control badge-primary">LEL  
  •         class="form-control" style="font-size:12px">@Html.DisplayFor(model => model.GasTesting.LEL)  
  •       
  •     class="col-1">  
  •         for="Toxic" class="form-control badge-primary">Toxic Gas  
  •         class="form-control" style="font-size:12px">@Html.DisplayFor(model => model.GasTesting.Toxic)  
  •       
  •   
  •  
     
    And OnPost procedure
     
    1. public async Task OnPostUpdateGasTestAsync()  
    2.         {  
    3.   
    4.             if (!ModelState.IsValid)  
    5.             {  
    6.                 return Page();  
    7.             }  
    8.   
    9.             var newGT = new GasTesting();  
    10.             if (await TryUpdateModelAsync(newGT, "GasTesting",   
    11.                     c => c.PTWNo,     
    12.                     c => c.DateGT,  
    13.                     c => c.NameGT,  
    14.                     c => c.EquipmentUsed,  
    15.                     c => c.Oxygen,  
    16.                     c => c.LEL,  
    17.                     c => c.Toxic))  
    18.             {  
    19.                 _context.GasTestings.Add(newGT);  
    20.                 await _context.SaveChangesAsync();  
    21.                 return Partial("Test/GasTest");  
    22.             }  
    23.   
    24.             if (newGT == null)  
    25.             {  
    26.                 return NotFound();  
    27.             }  
    28.   
    29.               
    30.   
    31.             return Partial("Test/GasTest");  
    32.         }  
     But when I run it I get
     
     
     
    I deliberatly have no query, just to make sure it is working 
    +2
  • Jenith Thakkar
    Let me show you one example

    This is your main view
    1. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  
    2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">script>  
    3. <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">script>  
    4.   
    5. <script type="text/javascript">  
    6.     $(function () {  
    7.         $(".nav_link").click(function () {  
    8.             //Custom attribute data_id is used to store the ID  
    9.             //Get the ID  
    10.             var id = $(this).attr("data_id");  
    11.             $.ajax({  
    12.                 url: '@Url.Action("DisplayEmployees", "MyTest")',  
    13.                 type: "post",  
    14.                 dataType: "html",  
    15.                 contentType: 'application/json; charset=utf-8',  
    16.                 data: JSON.stringify({ id: id }), //add parameter  
    17.                 success: function (data) {  
    18.                     //success  
    19.                     $('#ProductsDiv'+id).html(data); //populate the tab content.  
    20.                 },  
    21.                 error: function () {  
    22.                     alert("error");  
    23.                 }  
    24.             });  
    25.         });  
    26.     });  
    27. script>  
    28. <div class="container">  
    29.     <ul class="nav nav-tabs">  
    30.         <li class="active"><a data-toggle="tab" class="nav_link" data_id="1" href="#home">Homea>li>  
    31.         <li><a data-toggle="tab" class="nav_link" data_id="2" href="#menu1">Menu 1a>li>  
    32.         <li><a data-toggle="tab" class="nav_link" data_id="3" href="#menu2">Menu 2a>li>  
    33.     ul>  
    34.   
    35.     <div class="tab-content">  
    36.         <div id="home" class="tab-pane fade in active">  
    37.             <h3>HOMEh3>  
    38.             <p>Some content.p>  
    39.             <br />  
    40.             <div id="ProductsDiv1">  
    41.             div>  
    42.         div>  
    43.         <div id="menu1" class="tab-pane fade">  
    44.             <h3>Menu 1h3>  
    45.             <p>Some content in menu 1.p><br />  
    46.             <div id="ProductsDiv2">  
    47.             div>  
    48.         div>  
    49.         <div id="menu2" class="tab-pane fade">  
    50.             <h3>Menu 2h3>  
    51.             <p>Some content in menu 2.p><br />  
    52.             <div id="ProductsDiv3">  
    53.             div>  
    54.         div>  
    55.     div>  
    56. div>  
    In this main view you have to fetch content of partial view and replace here

    1. [HttpPost]  
    2.         public PartialViewResult DisplayEmployees(string id)  
    3.         {  
    4.             int empid = Convert.ToInt32(id);  
    5.             //According to the id to query the database  
    6.             var query = from ee in context.Employees  
    7.                         where ee.EmployeeID == empid  
    8.                         select ee;  
    9.             return PartialView("DisplayEmployeeWebGrid", query.ToList());  
    10.         }  
    So when you calling this method from ajax it will fetch html from partial view where DisplayEmployeeWebGrid is a partial view

    So in place of DisplayEmployeeWebGrid you have to write your tab name and create a partial view with that tab name 
    how to create it ? you can find it from here
    https://www.c-sharpcorner.com/UploadFile/ff2f08/partial-view-in-mvc/

    Thank you
    +2
  • Marius Vasile
    Thank you Jenith for your reply, I haven't tryed partial view, if you can develop a little on the subject or you have a link from where I can learn more it will be appreciated
    +2
  • Jenith Thakkar
    Have you created views for every tabs 
    if yes then convert it to partial views so you can render only that tab
    +2