Zaffar Khan

Zaffar Khan

  • 1.7k
  • 18
  • 12.6k

Refresh Asp.Net MVC Partial View using Ajax

Mar 2 2021 2:51 PM
I am using ajax call for deleting the record, and after success I want to refresh the table record but without loading the whole page. So I have created a Partial View for that.
 
My Partial View Code:
  1. @model IEnumerable<MyAppModels.StudentModel>    
  2.     
  3. <table class="table">    
  4.     <tr>    
  5.         <th>    
  6.             @Html.DisplayNameFor(model => model.FName)    
  7.         </th>    
  8.         <th>    
  9.             @Html.DisplayNameFor(model => model.LName)    
  10.         </th>    
  11.         <th>    
  12.             @Html.DisplayNameFor(model => model.Email)    
  13.         </th>    
  14.         <th>    
  15.             @Html.DisplayNameFor(model => model.address.Details)    
  16.         </th>    
  17.         <th>    
  18.             @Html.DisplayNameFor(model => model.address.Country)    
  19.         </th>    
  20.         <th>    
  21.             @Html.DisplayNameFor(model => model.address.State)    
  22.         </th>    
  23.         <th></th>    
  24.     </tr>    
  25.     
  26.     @foreach (var item in Model)    
  27.     {    
  28.         <tr>    
  29.             <td>    
  30.                 <input type="hidden" name="stid" id="stid" value="@item.Id" />    
  31.                 @Html.DisplayFor(modelItem => item.FName)    
  32.             </td>    
  33.             <td>    
  34.                 @Html.DisplayFor(modelItem => item.LName)    
  35.             </td>    
  36.             <td>    
  37.                 @Html.DisplayFor(modelItem => item.Email)    
  38.             </td>    
  39.             <td>    
  40.                 @Html.DisplayFor(modelItem => item.address.Details)    
  41.             </td>    
  42.             <td>    
  43.                 @Html.DisplayFor(modelItem => item.address.Country)    
  44.             </td>    
  45.             <td>    
  46.                 @Html.DisplayFor(modelItem => item.address.State)    
  47.             </td>    
  48.             <td>    
  49.                 @Html.ActionLink("Edit""Edit"new { id = item.Id }) |    
  50.                 @Html.ActionLink("Details""Details"new { id = item.Id }) |    
  51.                 @Html.ActionLink("Delete""Delete"new { id = item.Id }, new { Id = item.Id, Name = "del" })    
  52.             </td>    
  53.         </tr>    
  54.     }    
  55.     
  56. </table>  
My Controller Code:
  1. public ActionResult AStudents()    
  2. {    
  3. var res = stude.GettAllStudents();    
  4. return PartialView("_StudentData",res);    
  5. }  
My Ajax/Js Code:
  1. // Delete record    
  2. var name=document.getElementsByName( "del" );    
  3. $(name).click(function (e) {    
  4.     e.preventDefault();    
  5.     var id = this.id;    
  6.     var url="Home/AStudents"    
  7.     $.ajax({    
  8.         method: "POST",    
  9.         url: "Delete/" + id,    
  10.         contentType: "application/json; charset=utf-8",    
  11.         cache: false,    
  12.         async: true,    
  13.           data: JSON.stringify(id),    
  14.         success: function (data) {    
  15.   
  16.             $("#strecord").load("AStudents");    
  17.             console.log(data);    
  18.   
  19.         },    
  20.         error: function (err) {    
  21.             console.log('Failed to get data' + err);    
  22.                
  23.         }    
  24.     });  
  25. });    
My View where I am calling the Partial View:
  1. @{    
  2.     ViewBag.Title = "AllStudents";    
  3. }    
  4.     
  5. <h2>AllStudents</h2>    
  6.     
  7. <p>    
  8.     @Html.ActionLink("Create New""Create")    
  9. </p>    
  10.     
  11. <div id="strecord" name="strecord">    
  12.     @{ Html.RenderPartial("_StudentData");}    
  13. </div>  
Output:
 
When I clicked the delete link, it deletes the record and loads the View as I want but the issue is that When I clicked on another record then it shows an error" record not found" and this is because of the view overload on the First view instead of refreshing it. I don't know how to refresh it, instead of overload. From overload, I mean that my ajax code load a Partial view on the already loaded view, and because of that the error occurs and stays until I refresh the page.
 
Any help will be appreciated.

Answers (3)