Bryan Gomez

Bryan Gomez

  • NA
  • 21
  • 36.8k

[MVC] - How to refresh partial view using AJAX GET method

Nov 13 2017 10:19 PM
I need to refresh my partial view in my MVC razor type with parameter from my DropDownList. The problem is that, it doens't refresh the table from my partial view at all. But it can capture the parameter. Technically, I just want to do a filtering from my table.
 
Here's my script:
  1. $("#toTech").change(function () {  
  2.     $.ajax({  
  3.         type: "GET",  
  4.         url: "/joballocation/TableData",  
  5.         data: { Tech: $(this).val() }  
  6.     });  
  7.   
  8. }); 
 If I put the acction success:
  1. $("#toTech").change(function () {  
  2.     $.ajax({  
  3.         type: "GET",  
  4.         url: "/joballocation/TableData",  
  5.         data: { Tech: $(this).val() }  
  6.     }).success(function () {  
  7.         $("#theTable").load("/joballocation/TableData");  
  8.     });  
  9.   
  10. }); 
 It calls again my controller but no parameter.
 
Here's my controller:

  1. private string tech;   
  2. [HttpGet]   
  3. public ActionResult TableData(string Tech)  
  4. {  
  5.     dynamic model = new ExpandoObject();  
  6.   
  7.     if (ModelState.IsValid)  
  8.     {  
  9.         if (Tech == "") { Tech = "NULL"; }  
  10.         tech = Tech.Replace(" ", String.Empty);  
  11.           
  12.         model.MyJob = GetMyJobs();  
  13.         model.TJob = RemoveMyJobs(tech);  
  14.     }  
  15.   
  16.     return PartialView("_joballocation", model);  
  17. }  

 My view:
  1. <div class="row">  
  2.     <div class="col-sm-4">  
  3.         <table class="table table-striped table-hover table-condensed" id="myJobTable">  
  4.             <thead class="thead-dark">  
  5.                 <tr>  
  6.                     <th scope="col">#</th>  
  7.                     <th scope="col"></th>  
  8.                     <th scope="col">Date</th>  
  9.                     <th scope="col">SO No</th>  
  10.                     <th scope="col">Serial</th>  
  11.                     <th scope="col">Status</th>  
  12.                     <th scope="col">Technician</th>  
  13.                 </tr>  
  14.             </thead>  
  15.             @{  
  16.                 var count = 1;  
  17.   
  18.                 foreach (MyJobAllocation tJob in Model.MyJob)  
  19.                 {  
  20.                 <tr>  
  21.                     <td>@tJob.row</td>  
  22.                     <td>  
  23.                         <input type="checkbox" id="chk@(count)" />  
  24.                     </td>  
  25.                     <td>@tJob.date</td>  
  26.                     <td>@tJob.so_no</td>  
  27.                     <td>@tJob.serial</td>  
  28.                     <td>@tJob.status</td>  
  29.                     <td>@tJob.technician</td>  
  30.                 </tr>  
  31.                           
  32.                     count += 1;  
  33.                 }   
  34.             }  
  35.         </table>  
  36.     </div>  
  37.     <div class="col-sm-1">  
  38.         <input type="button" value=">" onclick="asign_to()" />  
  39.     </div>  
  40.     <div class="w-100"></div>  
  41.     <div class="col-sm-1">  
  42.         <input type="button" value="<" />  
  43.     </div>  
  44.     <div class="col-sm-4">  
  45.         <table class="table table-striped table-hover table-condensed" id="ToJobTable">  
  46.             <thead class="thead-dark">  
  47.                 <tr>  
  48.                     <th scope="col">#</th>  
  49.                     <th scope="col"></th>  
  50.                     <th scope="col">Date</th>  
  51.                     <th scope="col">SO No</th>  
  52.                     <th scope="col">Serial</th>  
  53.                     <th scope="col">Status</th>  
  54.                     <th scope="col">Technician</th>  
  55.                 </tr>  
  56.             </thead>  
  57.             @{  
  58.                 count = 1;  
  59.   
  60.                 foreach (ToJobAllocation myJob in Model.TJob)  
  61.                 {  
  62.                 <tr>  
  63.                     <td>@myJob.row</td>  
  64.                     <td>  
  65.                         <input type="checkbox" id="chk@(count)" />  
  66.                     </td>  
  67.                     <td>@myJob.date</td>  
  68.                     <td>@myJob.so_no</td>  
  69.                     <td>@myJob.serial</td>  
  70.                     <td>@myJob.status</td>  
  71.                     <td>@myJob.technician</td>  
  72.                 </tr>  
  73.                           
  74.                     count += 1;  
  75.                 }   
  76.             }  
  77.         </table>  
  78.     </div>  
  79. </div>