Skip to content
Loading
Refresh Asp.Net MVC Partial View using Ajax Now for each action perform Ajax request , For Edit load a partial view , for Details load a partial view , for Delete just delete and load the whole html.
// For Edit  Ajax
  1. $("#Edit").click(function () {    
  2.               
  3.                var id = $(this).attr("data-Id");  
  4.              
  5.                $.ajax({    
  6.                    method: "POST",    
  7.                    url: "Home/Edit/" + id,    
  8.                    contentType: "application/json; charset=utf-8",    
  9.                    cache: false,    
  10.                    
  11.                    success: function (data) {    
  12.    
  13.                        $("#EditArea").html(data);   
  14.                       
  15.    
  16.                    },    
  17.                    error: function (err) {    
  18.                        console.log('Failed to get data' + err);    
  19.                           
  20.                    }    
  21.                });    
  22.    
  23.    
  24.            });   
 // controller action for Edit
 
  1. public ActionResult Edit(int id)      
  2. {      
  3. var res = stude.GettAllStudents().Where(x=>x.Id==id);      
  4. return partiaView(res);      
  5. }   
 // partial view for edit
  1.  @model MyAppModels.StudentModel  
  2. "/home/update">  
  3. Name:"text" value="@model.Name">  
  4. .......other fields.....  
  5. "submit">Update
  6.  
 // Ajax for Delete
 
  1. $("#Delete").click(function () {        
  2.                   
  3.                var id = $(this).attr("data-Id");      
  4.                  
  5.                $.ajax({        
  6.                    method: "POST",        
  7.                    url: "Home/Delete/" + id,        
  8.                    contentType: "application/json; charset=utf-8",        
  9.                    cache: false,        
  10.                        
  11.                    success: function (data) {        
  12.        
  13.                        $("#wrapper").Html(data);       
  14.                           
  15.        
  16.                    },        
  17.                    error: function (err) {        
  18.                        console.log('Failed to get data' + err);        
  19.                               
  20.                    }        
  21.                });        
  22.        
  23.        
  24.            });       
 // Action for Delete
 
  1. public ActionResult Delete(int id)          
  2. {          
  3. var Student= stude.GettAllStudents().Where(x=>x.Id==id);    
  4. _context.Students.Remove(Student);  
  5. _context.SaveChanges();      
  6. return View("GetStudents",res);          
  7. }     
  • Sachin Singh
    @Zaffar , as long as it is about html or js , it is always visible in console because they are browser dependent and there is no way to hide html. i.e the reason sensitive information are stored on backend like in a configuration file.
    and yes the code is safe , if you will click ctrl+u in this page (c# corner) then you can check all it's html and JS , this doesn't mean it is not safe.
    for authorization we have different things like OAuth, windows auth, forms auth etc.
  • Sachin Singh,
    The solution you provide worked but there is another issue, like when we open the console then we can see that on click the whole page and its code is showing. Is this the correct way? as I am new and confuse about the whole code is moving and showing in Console.
     
    Thanks.