Skip to content
Loading
Open Modal PopUp  
  •       
  •   
  • Js part:
    1.   
    And controller sample
     
    1. // GET: MyController/Delete  
    2. [HttpGet]  
    3. public ActionResult Delete(int id)  
    4. {  
    5.     var model = Context.my_models.Where(x => x.id == id).FirstOrDefault();  
    6.     if (model != null)  
    7.     {  
    8.         Context.my_models.DeleteOnSubmit(model);  
    9.         Context.SubmitChanges();  
    10.   
    11.         return RedirectToAction("List");  
    12.     }  
    13.   
    14.     return new HttpNotFoundResult();  
    15. }  
     
  • Hi Sachin
     
      I can't do like this using data-toggle & data-target. I have tried & it is working
     
    @if (item.IsActive)
    {
    }
    else
    {
    }
     
    Thanks 
  • Sachin Singh
    then do it as
    1. "#" onclick="OpenPopup(@item.Id); return false;" class='btn btn-danger btn-sm' id='btnDelete' style='margin-left:5px'>class='fa fa-trash'> Delete   
    step 2.
    1. class="modal fade" id="DeleteModal">  
    2. class="modal-dialog">  
    3. class="modal-content">  
    4. class="modal-header">  
    5. "#" class="close" data-dismiss="modal">×  
    6. class="modal-title">Delete Record  
      
  • class="modal-body">  
  • "hidden" id="txtId">  
  •  Are you sure you want to Delete this Record ? 
      
  •   
  • class="modal-footer">  
  • "#" class="btn btn-default" data-dismiss="modal">Cancel  
  • "#" class="btn btn-success" onclick="Delete()">Confirm  
  •   
  •   
  •   
  •   
  •    
  • step 3.
    1. function OpenPopUp(Id)  
    2. {  
    3. $("#txtId").val(Id);  
    4. $("#DeleteModal").Modal('open');  
    5.   
    6. }  
    step 4. 
    1. function Delete(e)  
    2. {  
    3. e.preventDefault();
    4. var Id= $("#txtId").val()  
    5. $.Post("/home/Delete?Id"+Id,function(data){  
    6. alert(data);  
    7. });  
    8. }  
     Note :- when using href="#" and onclick function then best practise is to use below code everywhere in the application
    1. $('#myLink').click(function(){ MyFunction(); return false; });  
     where MyFunction() is any function or directly your code like ajax post etc.
  • Hi Sachin
     
      I want to open Below Modal Popup & when user confirm here then record to be deleted
     
    Are you sure you want to Delete this Record ?
     
    Thanks 
  • Sachin Singh
    your code is correct, just pass the parameter as an object too.
     
     
    1. "Url.Action("Delete", "Product", new { MyId= @item.Id});" onclick="return confirm('Do you want to delete this record?')"    
    2. class='btn btn-danger btn-sm' id='btnDelete' style='margin-left:5px'>class='fa fa-trash'> Delete    
     
     Note:- I am intentionally named the Id as MyId because I wanted to show you that this name must match with your Delete Action parameter.
    1. public ActionResult Delete(int MyId)  
    2. {  
    3. // your code  
    4. return view();  
    5. }  
    next, the confirm dialog should open automatically, as your code is correct.